Bläddra i källkod

Fix services singleton instances not working properly

Lucas de Souza 1 år sedan
förälder
incheckning
758d18cd33

+ 7 - 6
main.js

@@ -1,14 +1,15 @@
-import { Config } from "./src/util/config";
-import { i18nHelper } from "./src/services/i18nHelper";
-import * as LocalizedStringsService from "./src/services/localizedStringsService";
 import { openAssessmentDetail, levenshteinDistance } from "./src/util/utils";
 import { processData } from "./src/util/dataProcess";
 import { parseExpression, parseCode } from "./src/util/parseFromVisual";
 import { autoGenerateTestCaseOutput } from "./src/util/auto_gen_output";
-import { IVProgProcessor } from "./src/processor/ivprogProcessor.js";
-import { SemanticAnalyser } from "./src/processor/semantic/semanticAnalyser.js";
+import { IVProgProcessor } from "./src/processor/ivprogProcessor";
+import { SemanticAnalyser } from "./src/processor/semantic/semanticAnalyser";
 import { Modes } from "./src/processor/modes";
 import { Location } from "./src/memory/location";
+import { Config } from "./src/util/config";
+import { i18nHelper } from "./src/services/i18nHelper";
+import {getInstance as LocalizedStrings} from "./src/services/localizedStringsService";
+import {LanguageService} from "./src/services/languageService";
 
 const Settings = {
   programming: [],
@@ -19,13 +20,13 @@ const Settings = {
 }
 
 const i18n = i18nHelper.i18n;
-const LocalizedStrings = LocalizedStringsService.getInstance();
 
 export {
   i18n,
   Settings,
   Config,
   LocalizedStrings,
+  LanguageService,
   SemanticAnalyser,
   IVProgProcessor,
   Modes,

Filskillnaden har hållts tillbaka eftersom den är för stor
+ 4296 - 3323
package-lock.json


+ 6 - 6
src/ast/error/syntaxErrorFactory.js

@@ -1,17 +1,17 @@
-import * as LocalizedStringsService from "./../../services/localizedStringsService";
+import {getInstance as LocalizedStrings} from "./../../services/localizedStringsService.js";
 import { SyntaxError } from "./syntaxError";
 
-const LocalizedStrings = LocalizedStringsService.getInstance();
 
 function createError (message_id, context = []) {
+  const error_message = LocalizedStrings().getError(message_id, context);
   return new SyntaxError(
-    LocalizedStrings.getError(message_id, context),
+    error_message,
     message_id
   );
 }
 
 export const SyntaxErrorFactory = Object.freeze({
-  extra_lines: () => new SyntaxError(LocalizedStrings.getError("extra_lines")),
+  extra_lines: () => new SyntaxError(LocalizedStrings().getError("extra_lines")),
   token_missing_one: (expected, token) => {
     const context = [expected, token.text, token.line, token.col];
     const error = createError("token_missing_one", context);
@@ -19,7 +19,7 @@ export const SyntaxErrorFactory = Object.freeze({
     return error;
   },
   token_missing_list: (expectedList, token) => {
-    const line = expectedList.join(LocalizedStrings.getOR());
+    const line = expectedList.join(LocalizedStrings().getOR());
     const error = SyntaxErrorFactory.token_missing_one(line, token);
     error.context = { line: token.line, column: token.col};
     return error;
@@ -73,7 +73,7 @@ export const SyntaxErrorFactory = Object.freeze({
     return error;
   },
   invalid_type: (list, token) => {
-    const line = list.join(LocalizedStrings.getOR());
+    const line = list.join(LocalizedStrings().getOR());
     const context = [token.text, token.line, token.col, line];
     const error = createError("invalid_type", context);
     error.context = { line: token.line, column: token.col};

+ 3 - 3
src/processor/ivprogProcessor.js

@@ -26,7 +26,7 @@ import { StoreValueRef } from "./store/value/store_value_ref";
 import { ArrayStoreValue } from "./store/value/array_store_value";
 import { ArrayStoreValueRef } from "./store/value/array_store_value_ref";
 import { StoreValueAddress } from "./store/value/store_value_address";
-import { LocalizedStrings } from "../services/localizedStringsService";
+import {getInstance as LocalizedStrings} from "../services/localizedStringsService";
 
 export class IVProgProcessor {
   static get MAIN_INTERNAL_ID () {
@@ -392,7 +392,7 @@ export class IVProgProcessor {
     ) {
       return store;
     } else if (this.mode === Modes.ABORT) {
-      throw LocalizedStrings.getMessage("aborted_execution");
+      throw LocalizedStrings().getMessage("aborted_execution");
     }
 
     if (cmd instanceof Commands.Declaration) {
@@ -1041,7 +1041,7 @@ export class IVProgProcessor {
       await Utils.sleep(3);
     }
     if (this.mode === Modes.ABORT) {
-      throw LocalizedStrings.getMessage("aborted_execution");
+      throw LocalizedStrings().getMessage("aborted_execution");
     }
     if (this.instruction_count >= Config.max_instruction_count) {
       throw new Error(

+ 5 - 5
src/services/i18nHelper.js

@@ -1,5 +1,5 @@
 import line_i18n from "line-i18n";
-import { LocalizedStrings } from "./localizedStringsService";
+import { getInstance as LocalizedStrings} from "./localizedStringsService";
 
 const StringTypes = line_i18n.StringTypes;
 
@@ -9,14 +9,14 @@ export const i18nHelper = Object.freeze({
     var type = opts[0].toLowerCase();
     var id = opts[1];
     if (StringTypes.ERROR === type) {
-      return LocalizedStrings.getError(id);
+      return LocalizedStrings().getError(id);
     } else if (StringTypes.MESSAGE === type) {
-      return LocalizedStrings.getMessage(id); 
+      return LocalizedStrings().getMessage(id); 
     } else if (StringTypes.UI === type) {
-      return LocalizedStrings.getUI(id);
+      return LocalizedStrings().getUI(id);
     } else {
       console.warn("A string has been passed to the i18n helper function that was not in the form type:id -> " + identifier);
-      return LocalizedStrings.getString(identifier, type);
+      return LocalizedStrings().getString(identifier, type);
     }
   }
 });

+ 0 - 46
src/services/languageService.js

@@ -1,46 +0,0 @@
-/* global iLMparameters*/
-import Lexers from "./../../grammar/";
-import line_i18n from "line-i18n";
-import { Config } from "./../util/config";
-
-class LanguageServiceExtended extends line_i18n.LanguageServiceNoLS {
-  constructor () {
-    super(
-      typeof iLMparameters === "undefined"
-        ? Config.default_lang
-        : iLMparameters.lang
-    );
-  }
-
-  getDefaultLang () {
-    return "en";
-  }
-
-  getCurrentLexer () {
-    const langInfo = Lexers[this.getLang()];
-    if (langInfo === null || langInfo === undefined) {
-      return Lexers[this.getDefaultLang()].lexer;
-    } else {
-      return langInfo.lexer;
-    }
-  }
-
-  getCurrentLangFuncs () {
-    const langInfo = Lexers[this.getLang()];
-    if (langInfo === null || langInfo === undefined) {
-      return Lexers[this.getDefaultLang()].langFuncs;
-    } else {
-      return langInfo.langFuncs;
-    }
-  }
-
-  getCurrentLangLibs () {
-    const langInfo = Lexers[this.getLang()];
-    if (langInfo === null || langInfo === undefined) {
-      return Lexers[this.getDefaultLang()].langLibs;
-    }
-    return langInfo.langLibs;
-  }
-}
-
-export const LanguageService = new LanguageServiceExtended();

+ 1 - 5
src/services/localizedStringsService.js

@@ -40,12 +40,8 @@ class IVProgLocalizedStrings extends line_i18n.LocalizedStrings {
   }
 }
 
-export const LocalizedStrings = Object.freeze(
-  new IVProgLocalizedStrings(LanguageService, Langs)
-);
-
 const _instance = new IVProgLocalizedStrings(LanguageService, Langs);
 
-export function getInstance () {
+export const getInstance =  () => {
   return _instance;
 }

+ 3 - 3
src/util/auto_gen_output.js

@@ -2,7 +2,7 @@ import {SemanticAnalyser} from './../processor/semantic/semanticAnalyser';
 import {IVProgProcessor} from './../processor/ivprogProcessor';
 import { InputTest } from './inputTest';
 import { OutputTest } from './outputTest';
-import { LocalizedStrings } from './../services/localizedStringsService'
+import { getInstance as LocalizedStrings } from './../services/localizedStringsService'
 
 export async function autoGenerateTestCaseOutput (program_text, testCases, callback) {
   let copyTestCases = testCases.map((test) => Object.assign({}, test));
@@ -23,11 +23,11 @@ export async function autoGenerateTestCaseOutput (program_text, testCases, callb
       const output_2 = result_1.program.output.list;
       const input_2 = result_1.program.input;
       if (input_2.index != input_2.inputList.length) {
-        window.showAlert(LocalizedStrings.getMessage("testcase_autogen_unused_input", [result_1.id + 1]));
+        window.showAlert(LocalizedStrings().getMessage("testcase_autogen_unused_input", [result_1.id + 1]));
         return false;
       }
       if (output_2.length == 0) {
-        window.showAlert(LocalizedStrings.getMessage("testcase_autogen_empty", [result_1.id + 1]));
+        window.showAlert(LocalizedStrings().getMessage("testcase_autogen_empty", [result_1.id + 1]));
       }
       copyTestCases[result_1.id].output = output_2;
     }

+ 2 - 2
src/util/inputTest.js

@@ -1,5 +1,5 @@
 import { Input } from './../io/input';
-import { LocalizedStrings } from '../services/localizedStringsService';
+import { getInstance as LocalizedStrings} from '../services/localizedStringsService';
 
 export class InputTest extends Input {
 
@@ -15,7 +15,7 @@ export class InputTest extends Input {
         resolve(this.inputList[this.index]);
         this.index++;
       } else {
-        reject(new Error(LocalizedStrings.getError("exceeded_input_request")));
+        reject(new Error(LocalizedStrings().getError("exceeded_input_request")));
       }
     });
     return promise

+ 3 - 2
src/util/utils.js

@@ -1,7 +1,8 @@
 import { LanguageService } from "./../services/languageService";
-import { LocalizedStrings } from "./../services/localizedStringsService";
+import { getInstance as LocalizedStrings } from "./../services/localizedStringsService";
 import { Operators } from "./../ast/operators";
 
+
 /**
  *
  * source: https://stackoverflow.com/a/16270434
@@ -63,7 +64,7 @@ function fillCache () {
     ];
     for (let op = 0; op < logicOpList.length; ++op) {
       const lOp = `logic_operator_${logicOpList[op]}`;
-      cacheOp.push(LocalizedStrings.getUI(lOp));
+      cacheOp.push(LocalizedStrings().getUI(lOp));
     }
   }
 }

+ 1 - 1
tsconfig.json

@@ -1,6 +1,6 @@
 {
   "compilerOptions": {
-    "target": "es2015",
+    "target": "es5",
     "noImplicitAny": true,
     "module": "commonjs",
     "declaration": true,

+ 1 - 1
webpack.config.babel.js

@@ -13,7 +13,7 @@ export default {
   module: {
     rules: [
       {
-        test: /\.(ts|js)?x?$/,
+        test: /\.(ts|js)x?$/,
         exclude: /(node_modules)/,
         use: {
           loader: "babel-loader",