Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/bundles/repl/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,28 @@ export function set_evaluator(evalFunc: Function) {
throw new Error(`Wrong parameter type "${typeName}' in function "set_evaluator". It supposed to be a function and it's the entrance function of your metacircular evaulator.`);
}
INSTANCE.evalFunction = evalFunc;
INSTANCE.resultCallback = undefined;
return {
toReplString: () => '<Programmable Repl Initialized>',
};
}

/**
* Sets evaluator with jslang, then calls callback when results received.
*
* @param callback Callback with return value
*/
export function set_js_slang_evaluator_with_callback(
callback: (_: any) => void,
) {
INSTANCE.evalFunction = default_js_slang;
INSTANCE.resultCallback = callback;
return {
toReplString: () => '<Programmable Repl Initialized>',
};
}



/**
* Display message in Programmable Repl Tab
Expand Down
1 change: 1 addition & 0 deletions src/bundles/repl/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

export {
set_evaluator,
set_js_slang_evaluator_with_callback,
repl_display,
set_background_image,
set_font_size,
Expand Down
2 changes: 2 additions & 0 deletions src/bundles/repl/programmable_repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { COLOR_RUN_CODE_RESULT, COLOR_ERROR_MESSAGE, DEFAULT_EDITOR_HEIGHT } fro

export class ProgrammableRepl {
public evalFunction: Function;
public resultCallback: ((result: any) => void) | undefined;
public userCodeInEditor: string;
public outputStrings: any[];
private _editorInstance;
Expand Down Expand Up @@ -203,6 +204,7 @@ export class ProgrammableRepl {
if (evalResult.status !== 'error') {
this.pushOutputString('js-slang program finished with value:', COLOR_RUN_CODE_RESULT);
// Here must use plain text output mode because evalResult.value contains strings from the users.
this.resultCallback?.(evalResult.value);
this.pushOutputString(evalResult.value === undefined ? 'undefined' : evalResult.value.toString(), COLOR_RUN_CODE_RESULT);
} else {
const errors = context.errors;
Expand Down