From 2a199c39a36cff580067b37b537c31e0862d3703 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Tue, 5 Jan 2021 07:47:48 -0300 Subject: [PATCH] fix(basic.gblib): Improving error handling in BASIC. --- packages/basic.gblib/services/GBVMService.ts | 20 +++++++++++++++++++- src/app.ts | 1 + 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/packages/basic.gblib/services/GBVMService.ts b/packages/basic.gblib/services/GBVMService.ts index 5285cb2f..1a79b939 100644 --- a/packages/basic.gblib/services/GBVMService.ts +++ b/packages/basic.gblib/services/GBVMService.ts @@ -697,15 +697,34 @@ export class GBVMService extends GBService { ); } + /** + * Executes the converted JavaScript from BASIC code inside execution context. + */ public static async callVM(text: string, min: GBMinInstance, step: GBDialogStep, deployer: GBDeployer) { + + // Creates a class DialogKeywords which is the *this* pointer + // in BASIC. + const sandbox: DialogKeywords = new DialogKeywords(min, deployer); + + // Injects the .gbdialog generated code into the VM. + const context = vm.createContext(sandbox); const code = min.sandBoxMap[text]; vm.runInContext(code, context); + // Tries to find the method related to this call. + const mainMethod = text.toLowerCase(); + if (!sandbox[mainMethod]) { + GBLog.error(`BASIC: Associated '${mainMethod}' dialog not found. Verify if .gbdialog is correctly published.`); + + return null; + } sandbox[mainMethod].bind(sandbox); + // Calls the function. + let ret = null; try { ret = await sandbox[mainMethod](step); @@ -713,7 +732,6 @@ export class GBVMService extends GBService { } catch (error) { GBLog.error(`BASIC ERROR: ${error.message} ${error.stack}`); } - return ret; } } diff --git a/src/app.ts b/src/app.ts index 479f37be..0e5fd67f 100644 --- a/src/app.ts +++ b/src/app.ts @@ -79,6 +79,7 @@ export class GBServer { */ public static run() { + GBLog.info(`The Bot Server is in STARTING mode...`); GBServer.globals = new RootData(); GBConfigService.init();