Fix syntax error at `%`

This commit is contained in:
Samuel Elliott 2018-07-20 21:10:44 +01:00
parent 5301498978
commit 19bbd4215c
No known key found for this signature in database
GPG Key ID: 8420C7CDE43DC4D6
1 changed files with 27 additions and 17 deletions

View File

@ -121,41 +121,51 @@ export class IterableWeakCollections {
} }
static _init() { static _init() {
const v8 = process.binding('v8'); this._natives_syntax = process.execArgv.some(s => /^--allow[-_]natives[-_]syntax$/.test(s));
// Enable natives syntax
v8.setFlagsFromString('--allow_natives_syntax');
// Use eval otherwise webpack will throw a syntax error // Use eval otherwise webpack will throw a syntax error
// eslint-disable-next-line no-eval // eslint-disable-next-line no-eval
this._getWeakMapEntries = eval(`(function (wm, max) { this._getWeakMapEntries = eval(`this._callWithNativesSyntax.bind(this, function (wm, max) {
// Retrieve all WeakMap instance key / value pairs up to \`max\`. \`max\` limits the // Retrieve all WeakMap instance key / value pairs up to \`max\`. \`max\` limits the
// number of key / value pairs returned. Make sure it is a positive number, // number of key / value pairs returned. Make sure it is a positive number,
// otherwise V8 aborts. Passing through \`0\` returns all elements. // otherwise V8 aborts. Passing through \`0\` returns all elements.
if (!(wm instanceof WeakMap)) throw new Error('weakmap must be an instance of WeakMap'); if (!(wm instanceof WeakMap)) throw new Error('weakmap must be an instance of WeakMap');
return %GetWeakMapEntries(wm, typeof max === 'number' && max > 0 ? max : 0); return %GetWeakMapEntries(wm, typeof max === 'number' && max > 0 ? max : 0);
})`); }, this)`);
// eslint-disable-next-line no-eval // eslint-disable-next-line no-eval
this._getWeakSetValues = eval(`(function (ws, max) { this._getWeakSetValues = eval(`this._callWithNativesSyntax.bind(this, function (ws, max) {
// Retrieve all WeakSet instance values up to \`max\`. \`max\` limits the // Retrieve all WeakSet instance values up to \`max\`. \`max\` limits the
// number of values returned. Make sure it is a positive number, // number of values returned. Make sure it is a positive number,
// otherwise V8 aborts. Passing through \`0\` returns all elements. // otherwise V8 aborts. Passing through \`0\` returns all elements.
if (!(ws instanceof WeakSet)) throw new Error('weakset must be an instance of WeakSet'); if (!(ws instanceof WeakSet)) throw new Error('weakset must be an instance of WeakSet');
return %GetWeakSetValues(ws, typeof max === 'number' && max > 0 ? max : 0); return %GetWeakSetValues(ws, typeof max === 'number' && max > 0 ? max : 0);
})`); }, this)`);
}
// Warm up the map and set iterator preview functions. V8 compiles static _callWithNativesSyntax(f, bind, ...args) {
// functions lazily (unless --nolazy is set) so we need to do this const v8 = process.binding('v8');
// before we turn off --allow_natives_syntax again.
this._getWeakMapEntries(new WeakMap(), 1); // Enable natives syntax
this._getWeakSetValues(new WeakSet(), 1); v8.setFlagsFromString('--allow_natives_syntax');
try {
const r = f.apply(bind, args);
// Disable --allow_natives_syntax again unless it was explicitly // Disable --allow_natives_syntax again unless it was explicitly
// specified on the command line // specified on the command line
if (!process.execArgv.some(s => /^--allow[-_]natives[-_]syntax$/.test(s))) v8.setFlagsFromString('--noallow_natives_syntax'); if (!this._natives_syntax) v8.setFlagsFromString('--noallow_natives_syntax');
return r;
} catch (err) {
// Disable --allow_natives_syntax again unless it was explicitly
// specified on the command line
if (!this._natives_syntax) v8.setFlagsFromString('--noallow_natives_syntax');
throw err;
}
} }
} }
IterableWeakCollections._init(); IterableWeakCollections._callWithNativesSyntax(IterableWeakCollections._init, IterableWeakCollections);