Skip to content

Commit

Permalink
REPL scripts runInThisContext (#584)
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey authored May 6, 2018
1 parent 3a40962 commit 7a58901
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ function evalAndExit (code: string, isPrinted: boolean) {
let result: any

try {
result = _eval(code, global)
result = _eval(code)
} catch (error) {
if (error instanceof TSError) {
console.error(printError(error))
Expand All @@ -190,7 +190,7 @@ function evalAndExit (code: string, isPrinted: boolean) {
/**
* Evaluate the code snippet.
*/
function _eval (input: string, context: any) {
function _eval (input: string) {
const lines = EVAL_INSTANCE.lines
const isCompletion = !/\n$/.test(input)
const undo = appendEval(input)
Expand All @@ -213,17 +213,17 @@ function _eval (input: string, context: any) {
}

return changes.reduce((result, change) => {
return change.added ? exec(change.value, EVAL_FILENAME, context) : result
return change.added ? exec(change.value, EVAL_FILENAME) : result
}, undefined)
}

/**
* Execute some code.
*/
function exec (code: string, filename: string, context: any) {
function exec (code: string, filename: string) {
const script = new Script(code, { filename: filename })

return script.runInNewContext(context)
return script.runInThisContext()
}

/**
Expand All @@ -234,8 +234,9 @@ function startRepl () {
prompt: '> ',
input: process.stdin,
output: process.stdout,
terminal: process.stdout.isTTY,
eval: replEval,
useGlobal: false
useGlobal: true
})

// Bookmark the point where we should reset the REPL state.
Expand All @@ -245,7 +246,7 @@ function startRepl () {
resetEval()

// Hard fix for TypeScript forcing `Object.defineProperty(exports, ...)`.
exec('exports = module.exports', EVAL_FILENAME, (repl as any).context)
exec('exports = module.exports', EVAL_FILENAME)
}

reset()
Expand Down Expand Up @@ -273,7 +274,7 @@ function startRepl () {
/**
* Eval code from the REPL.
*/
function replEval (code: string, context: any, _filename: string, callback: (err?: Error, result?: any) => any) {
function replEval (code: string, _context: any, _filename: string, callback: (err?: Error, result?: any) => any) {
let err: any
let result: any

Expand All @@ -284,7 +285,7 @@ function replEval (code: string, context: any, _filename: string, callback: (err
}

try {
result = _eval(code, context)
result = _eval(code)
} catch (error) {
if (error instanceof TSError) {
// Support recoverable compilations using >= node 6.
Expand Down

0 comments on commit 7a58901

Please sign in to comment.