Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 547
feat: add REPL top level await support#1383
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
e8dc156cb0b9f095b33853e2e9ab0335a36907a25bb8fe6aae3063c4ec5611dd1c667bd2b2ee040060c3ce11fecf8a89a04032134621bd639cba09922f9b4f1e1f793ff39161b8d44eaa4310187f0e1774bca34c5ad56a240d78e1784c46ed24fe2369f2977af297a4bdfbb91fd49ccad50b99119d1589cf816174e505e26d49e8a072064e3c11fc9a3c488e9abb693f81a5ef261e82fe45d2ed3a5ed7222efd1b64f04d9ce3487ee6c6433507fcd3ef269385b0fc3981e5e71b68ed6f48778ad31424ad3ac743be35f1d62c09d2d2c1e8bf1418bd3629a4cc6a102a7c39640e05cb7b8051f9e14788cb34aaac4c8c84ef735bee32f53542334895011fa5bdd8a772abc44a3d6781a886d50a80eaa31ffc3a6562d2e276b96d0b486e985fd0fae10d4File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,34 @@ | ||
| module.exports = { | ||
| ArrayFrom: Array.from, | ||
| ArrayIsArray: Array.isArray, | ||
| ArrayPrototypeJoin: (obj, separator) => Array.prototype.join.call(obj, separator), | ||
| ArrayPrototypeShift: (obj) => Array.prototype.shift.call(obj), | ||
| ArrayPrototypeForEach: (arr, ...rest) => Array.prototype.forEach.apply(arr, rest), | ||
| ArrayPrototypeIncludes: (arr, ...rest) => Array.prototype.includes.apply(arr, rest), | ||
| ArrayPrototypeJoin: (arr, ...rest) => Array.prototype.join.apply(arr, rest), | ||
| ArrayPrototypePop: (arr, ...rest) => Array.prototype.pop.apply(arr, rest), | ||
| ArrayPrototypePush: (arr, ...rest) => Array.prototype.push.apply(arr, rest), | ||
| FunctionPrototype: Function.prototype, | ||
| JSONParse: JSON.parse, | ||
| JSONStringify: JSON.stringify, | ||
| ObjectFreeze: Object.freeze, | ||
| ObjectKeys: Object.keys, | ||
| ObjectGetOwnPropertyNames: Object.getOwnPropertyNames, | ||
| ObjectDefineProperty: Object.defineProperty, | ||
| ObjectPrototypeHasOwnProperty: (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop), | ||
| RegExpPrototypeTest: (obj, string) => RegExp.prototype.test.call(obj, string), | ||
| RegExpPrototypeSymbolReplace: (obj, ...rest) => RegExp.prototype[Symbol.replace].apply(obj, rest), | ||
| SafeMap: Map, | ||
| SafeSet: Set, | ||
| StringPrototypeEndsWith: (str, ...rest) => String.prototype.endsWith.apply(str, rest), | ||
| StringPrototypeIncludes: (str, ...rest) => String.prototype.includes.apply(str, rest), | ||
| StringPrototypeLastIndexOf: (str, ...rest) => String.prototype.lastIndexOf.apply(str, rest), | ||
| StringPrototypeIndexOf: (str, ...rest) => String.prototype.indexOf.apply(str, rest), | ||
| StringPrototypeRepeat: (str, ...rest) => String.prototype.repeat.apply(str, rest), | ||
| StringPrototypeReplace: (str, ...rest) => String.prototype.replace.apply(str, rest), | ||
| StringPrototypeSlice: (str, ...rest) => String.prototype.slice.apply(str, rest), | ||
| StringPrototypeSplit: (str, ...rest) => String.prototype.split.apply(str, rest), | ||
| StringPrototypeStartsWith: (str, ...rest) => String.prototype.startsWith.apply(str, rest), | ||
| StringPrototypeSubstr: (str, ...rest) => String.prototype.substr.apply(str, rest), | ||
| SyntaxError: SyntaxError | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,254 @@ | ||
| // copied from https://github.com/nodejs/node/blob/88799930794045795e8abac874730f9eba7e2300/lib/internal/repl/await.js | ||
| 'use strict'; | ||
| const { | ||
| ArrayFrom, | ||
| ArrayPrototypeForEach, | ||
| ArrayPrototypeIncludes, | ||
| ArrayPrototypeJoin, | ||
| ArrayPrototypePop, | ||
| ArrayPrototypePush, | ||
| FunctionPrototype, | ||
| ObjectKeys, | ||
| RegExpPrototypeSymbolReplace, | ||
| StringPrototypeEndsWith, | ||
| StringPrototypeIncludes, | ||
| StringPrototypeIndexOf, | ||
| StringPrototypeRepeat, | ||
| StringPrototypeSplit, | ||
| StringPrototypeStartsWith, | ||
| SyntaxError, | ||
| } = require('./node-primordials'); | ||
| const parser = require('acorn').Parser; | ||
| const walk = require('acorn-walk'); | ||
| const { Recoverable } = require('repl'); | ||
| function isTopLevelDeclaration(state) { | ||
| return state.ancestors[state.ancestors.length - 2] === state.body; | ||
| } | ||
| const noop = FunctionPrototype; | ||
| const visitorsWithoutAncestors = { | ||
| ClassDeclaration(node, state, c) { | ||
| if (isTopLevelDeclaration(state)) { | ||
| state.prepend(node, `${node.id.name}=`); | ||
| ArrayPrototypePush( | ||
| state.hoistedDeclarationStatements, | ||
| `let ${node.id.name}; ` | ||
| ); | ||
| } | ||
| walk.base.ClassDeclaration(node, state, c); | ||
| }, | ||
| ForOfStatement(node, state, c) { | ||
| if (node.await === true) { | ||
| state.containsAwait = true; | ||
| } | ||
| walk.base.ForOfStatement(node, state, c); | ||
| }, | ||
| FunctionDeclaration(node, state, c) { | ||
| state.prepend(node, `${node.id.name}=`); | ||
| ArrayPrototypePush( | ||
| state.hoistedDeclarationStatements, | ||
| `var ${node.id.name}; ` | ||
| ); | ||
| }, | ||
| FunctionExpression: noop, | ||
| ArrowFunctionExpression: noop, | ||
| MethodDefinition: noop, | ||
| AwaitExpression(node, state, c) { | ||
| state.containsAwait = true; | ||
| walk.base.AwaitExpression(node, state, c); | ||
| }, | ||
| ReturnStatement(node, state, c) { | ||
| state.containsReturn = true; | ||
| walk.base.ReturnStatement(node, state, c); | ||
| }, | ||
| VariableDeclaration(node, state, c) { | ||
| const variableKind = node.kind; | ||
| const isIterableForDeclaration = ArrayPrototypeIncludes( | ||
| ['ForOfStatement', 'ForInStatement'], | ||
| state.ancestors[state.ancestors.length - 2].type | ||
| ); | ||
| if (variableKind === 'var' || isTopLevelDeclaration(state)) { | ||
| state.replace( | ||
| node.start, | ||
| node.start + variableKind.length + (isIterableForDeclaration ? 1 : 0), | ||
| variableKind === 'var' && isIterableForDeclaration ? | ||
| '' : | ||
| 'void' + (node.declarations.length === 1 ? '' : ' (') | ||
| ); | ||
| if (!isIterableForDeclaration) { | ||
| ArrayPrototypeForEach(node.declarations, (decl) => { | ||
| state.prepend(decl, '('); | ||
| state.append(decl, decl.init ? ')' : '=undefined)'); | ||
| }); | ||
| if (node.declarations.length !== 1) { | ||
| state.append(node.declarations[node.declarations.length - 1], ')'); | ||
| } | ||
| } | ||
| const variableIdentifiersToHoist = [ | ||
| ['var', []], | ||
| ['let', []], | ||
| ]; | ||
| function registerVariableDeclarationIdentifiers(node) { | ||
| switch (node.type) { | ||
| case 'Identifier': | ||
| ArrayPrototypePush( | ||
| variableIdentifiersToHoist[variableKind === 'var' ? 0 : 1][1], | ||
| node.name | ||
| ); | ||
| break; | ||
| case 'ObjectPattern': | ||
| ArrayPrototypeForEach(node.properties, (property) => { | ||
| registerVariableDeclarationIdentifiers(property.value); | ||
| }); | ||
| break; | ||
| case 'ArrayPattern': | ||
| ArrayPrototypeForEach(node.elements, (element) => { | ||
| registerVariableDeclarationIdentifiers(element); | ||
| }); | ||
| break; | ||
| } | ||
| } | ||
| ArrayPrototypeForEach(node.declarations, (decl) => { | ||
| registerVariableDeclarationIdentifiers(decl.id); | ||
| }); | ||
| ArrayPrototypeForEach( | ||
| variableIdentifiersToHoist, | ||
| ({ 0: kind, 1: identifiers }) => { | ||
| if (identifiers.length > 0) { | ||
| ArrayPrototypePush( | ||
| state.hoistedDeclarationStatements, | ||
| `${kind} ${ArrayPrototypeJoin(identifiers, ', ')}; ` | ||
| ); | ||
| } | ||
| } | ||
| ); | ||
| } | ||
| walk.base.VariableDeclaration(node, state, c); | ||
| } | ||
| }; | ||
| const visitors = {}; | ||
| for (const nodeType of ObjectKeys(walk.base)) { | ||
| const callback = visitorsWithoutAncestors[nodeType] || walk.base[nodeType]; | ||
| visitors[nodeType] = (node, state, c) => { | ||
| const isNew = node !== state.ancestors[state.ancestors.length - 1]; | ||
| if (isNew) { | ||
| ArrayPrototypePush(state.ancestors, node); | ||
| } | ||
| callback(node, state, c); | ||
| if (isNew) { | ||
| ArrayPrototypePop(state.ancestors); | ||
| } | ||
| }; | ||
| } | ||
| function processTopLevelAwait(src) { | ||
| const wrapPrefix = '(async () => { '; | ||
| const wrapped = `${wrapPrefix}${src} })()`; | ||
| const wrappedArray = ArrayFrom(wrapped); | ||
| let root; | ||
| try { | ||
| root = parser.parse(wrapped, { ecmaVersion: 'latest' }); | ||
| } catch (e) { | ||
| if (StringPrototypeStartsWith(e.message, 'Unterminated ')) | ||
| throw new Recoverable(e); | ||
| // If the parse error is before the first "await", then use the execution | ||
| // error. Otherwise we must emit this parse error, making it look like a | ||
| // proper syntax error. | ||
| const awaitPos = StringPrototypeIndexOf(src, 'await'); | ||
| const errPos = e.pos - wrapPrefix.length; | ||
| if (awaitPos > errPos) | ||
| return null; | ||
| // Convert keyword parse errors on await into their original errors when | ||
| // possible. | ||
| if (errPos === awaitPos + 6 && | ||
| StringPrototypeIncludes(e.message, 'Expecting Unicode escape sequence')) | ||
| return null; | ||
| if (errPos === awaitPos + 7 && | ||
| StringPrototypeIncludes(e.message, 'Unexpected token')) | ||
| return null; | ||
| const line = e.loc.line; | ||
| const column = line === 1 ? e.loc.column - wrapPrefix.length : e.loc.column; | ||
| let message = '\n' + StringPrototypeSplit(src, '\n')[line - 1] + '\n' + | ||
| StringPrototypeRepeat(' ', column) + | ||
| '^\n\n' + RegExpPrototypeSymbolReplace(/ \([^)]+\)/, e.message, ''); | ||
| // V8 unexpected token errors include the token string. | ||
| if (StringPrototypeEndsWith(message, 'Unexpected token')) | ||
| message += " '" + | ||
| // Wrapper end may cause acorn to report error position after the source | ||
| ((src.length - 1) >= (e.pos - wrapPrefix.length) | ||
| ? src[e.pos - wrapPrefix.length] | ||
| : src[src.length - 1]) + | ||
| "'"; | ||
| // eslint-disable-next-line no-restricted-syntax | ||
| throw new SyntaxError(message); | ||
| } | ||
| const body = root.body[0].expression.callee.body; | ||
| const state = { | ||
| body, | ||
| ancestors: [], | ||
| hoistedDeclarationStatements: [], | ||
| replace(from, to, str) { | ||
| for (let i = from; i < to; i++) { | ||
| wrappedArray[i] = ''; | ||
| } | ||
| if (from === to) str += wrappedArray[from]; | ||
| wrappedArray[from] = str; | ||
| }, | ||
| prepend(node, str) { | ||
| wrappedArray[node.start] = str + wrappedArray[node.start]; | ||
| }, | ||
| append(node, str) { | ||
| wrappedArray[node.end - 1] += str; | ||
| }, | ||
| containsAwait: false, | ||
| containsReturn: false | ||
| }; | ||
| walk.recursive(body, state, visitors); | ||
| // Do not transform if | ||
| // 1. False alarm: there isn't actually an await expression. | ||
| // 2. There is a top-level return, which is not allowed. | ||
| if (!state.containsAwait || state.containsReturn) { | ||
| return null; | ||
| } | ||
| const last = body.body[body.body.length - 1]; | ||
| if (last.type === 'ExpressionStatement') { | ||
| // For an expression statement of the form | ||
| // ( expr ) ; | ||
| // ^^^^^^^^^^ // last | ||
| // ^^^^ // last.expression | ||
| // | ||
| // We do not want the left parenthesis before the `return` keyword; | ||
| // therefore we prepend the `return (` to `last`. | ||
| // | ||
| // On the other hand, we do not want the right parenthesis after the | ||
| // semicolon. Since there can only be more right parentheses between | ||
| // last.expression.end and the semicolon, appending one more to | ||
| // last.expression should be fine. | ||
| state.prepend(last, 'return ('); | ||
| state.append(last.expression, ')'); | ||
| } | ||
| return ( | ||
| ArrayPrototypeJoin(state.hoistedDeclarationStatements, '') + | ||
| ArrayPrototypeJoin(wrappedArray, '') | ||
| ); | ||
| } | ||
| module.exports = { | ||
| processTopLevelAwait | ||
| }; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.