fix: not declared
errors in source native variant REPL
#1479
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #1477
Description
When using the REPL with the Source Native variant,
UndefinedVariable
errors will be raised when trying to reference previously declared functions in the REPL.This happens when a user first runs a program in the editor (via the "play" button) and subsequently tries to access the functions/variables in the REPL.
The Cause
UndefinedVariable
ErrorsThe bug is due to the function and variable identifiers not being properly kept tracked of during the transpilation stage of a Source Native program.
Declared functions and variables in a program are not added to the context (in
context.nativeStorage
) when executing a program. Hence, upon accessing previously declared functions/variables, anUndefinedVariable
error will be raised as those declarations have been lost.The fix would be add those declarations during the transpilation stage of a Source Native program:
js-slang/src/transpiler/transpiler.ts
Lines 682 to 687 in bf698f1
Why does
f;
not result in aUndefinedVariable
error?The bug is due to the use of
getIdentifiersInProgram
during the pre-evaluation stage of theFullJSRunner
:js-slang/src/runner/fullJSRunner.ts
Lines 73 to 75 in afcff0d
The snippet above adds all identifiers in the program, which includes identifiers of arguments in a function declaration, into the program context.
It turns out some of the arguments we have in our
stdlib
functions are namedf
:js-slang/src/stdlib/list.prelude.ts
Lines 44 to 51 in bf698f1
f
works in Source Native 2-4 since thelist
is introduced in those chapters and not in Source Native 1.