diff --git a/test262/skip_tests.json b/test262/skip_tests.json index e46ee4e350ab18792531716e285694a71c78172d..e8b3a705d72b752deae9ecb47e3722f627a62fd9 100644 --- a/test262/skip_tests.json +++ b/test262/skip_tests.json @@ -1035,54 +1035,9 @@ ] }, { - "reason": "Not support function.toString() due to Runtime can not obtain Source Code yet.", + "reason": "js-runtime incorrectly handling", "files": [ - "built-ins/RegExp/prototype/exec/S15.10.6.2_A1_T9.js", - "built-ins/RegExp/prototype/test/S15.10.6.3_A1_T9.js", - "built-ins/String/prototype/trim/15.5.4.20-2-46.js", - "built-ins/GeneratorPrototype/constructor.js", - "language/expressions/new.target/value-via-reflect-construct.js", - "language/expressions/new.target/asi.js", - "language/expressions/new.target/value-via-new.js", - "language/expressions/new.target/value-via-call.js", - "language/expressions/addition/S11.6.1_A2.2_T3.js", - "language/expressions/addition/S11.6.1_A3.2_T1.2.js", - "language/expressions/bitwise-and/S11.10.1_A3_T1.5.js", - "language/expressions/bitwise-not/S11.4.8_A3_T5.js", - "language/expressions/bitwise-or/S11.10.3_A3_T1.5.js", - "language/expressions/bitwise-xor/S11.10.2_A3_T1.5.js", - "language/expressions/division/S11.5.2_A3_T1.5.js", - "language/expressions/greater-than/S11.8.2_A3.2_T1.2.js", - "language/expressions/greater-than-or-equal/S11.8.4_A3.2_T1.2.js", - "language/expressions/left-shift/S11.7.1_A3_T1.5.js", - "language/expressions/less-than/S11.8.1_A3.2_T1.2.js", - "language/expressions/less-than-or-equal/S11.8.3_A3.2_T1.2.js", - "language/expressions/modulus/S11.5.3_A3_T1.5.js", - "language/expressions/multiplication/S11.5.1_A3_T1.5.js", - "language/expressions/postfix-decrement/S11.3.2_A3_T5.js", - "language/expressions/postfix-decrement/S11.3.2_A4_T5.js", - "language/expressions/postfix-increment/S11.3.1_A3_T5.js", - "language/expressions/postfix-increment/S11.3.1_A4_T5.js", - "language/expressions/prefix-decrement/S11.4.5_A3_T5.js", - "language/expressions/prefix-decrement/S11.4.5_A4_T5.js", - "language/expressions/prefix-increment/S11.4.4_A3_T5.js", - "language/expressions/prefix-increment/S11.4.4_A4_T5.js", - "language/rest-parameters/with-new-target.js", - "language/expressions/right-shift/S11.7.2_A3_T1.5.js", - "language/expressions/subtraction/S11.6.2_A3_T1.5.js", - "language/expressions/unary-minus/S11.4.7_A3_T5.js", - "language/expressions/unary-plus/S11.4.6_A3_T5.js", - "language/expressions/unsigned-right-shift/S11.7.3_A3_T1.5.js", - "language/module-code/eval-gtbndng-indirect-update-dflt.js", - "language/statements/switch/S12.11_A1_T4.js", - "language/statements/switch/S12.11_A1_T4.js", - "built-ins/Promise/resolve/ctx-ctor.js", - "built-ins/Promise/reject/ctx-ctor.js", - "built-ins/Promise/race/ctx-ctor.js", - "built-ins/Promise/all/ctx-ctor.js", - "built-ins/TypedArrayConstructors/ctors/length-arg/use-custom-proto-if-object.js", - "built-ins/TypedArrayConstructors/ctors/no-args/use-custom-proto-if-object.js", - "built-ins/TypedArrayConstructors/ctors/object-arg/use-custom-proto-if-object.js" + "built-ins/GeneratorPrototype/constructor.js" ] }, { diff --git a/ts2panda/scripts/generate_js_bytecode.py b/ts2panda/scripts/generate_js_bytecode.py index 2eb149f122824c5d56f354b8cc4b763914af0d35..f0935d561559137caad712d42d7527a8180853b8 100755 --- a/ts2panda/scripts/generate_js_bytecode.py +++ b/ts2panda/scripts/generate_js_bytecode.py @@ -44,6 +44,8 @@ def parse_args(): help='whether is commonjs') parser.add_argument("--q", action='store_true', help='whether is d.ts') + parser.add_argument("--functionSourceCode", action='store_true', + help='compile abc with function sourcecode info') arguments = parser.parse_args() return arguments @@ -92,6 +94,8 @@ def gen_abc_info(input_arguments): cmd.insert(5, '-c') if input_arguments.q: cmd.insert(6, '-q') + if input_arguments.functionSourceCode: + cmd.insert(7, '--function-sourcecode') run_command(cmd, path) diff --git a/ts2panda/src/cmdOptions.ts b/ts2panda/src/cmdOptions.ts index 026287aa0c233b6df56509d8b8c52dc9f4398a3e..fe304a5613c2756801d2ad8b4b45fff351eeeae9 100644 --- a/ts2panda/src/cmdOptions.ts +++ b/ts2panda/src/cmdOptions.ts @@ -45,7 +45,8 @@ const ts2pandaOptions = [ { name: 'dts-type-record', alias: 'q', type: Boolean, defaultValue: false, description: "Record type info for .d.ts files. Default: false" }, { name: 'debug-type', alias: 'g', type: Boolean, defaultValue: false, description: "Print type-related log. Default: false" }, { name: 'output-type', type: Boolean, defaultValue: false, description: "set output type."}, - { name: 'display-typeinfo', type: Boolean, defaultValue: false, description: "Display typeinfo of pairs of instruction orders and types when enable-typeinfo is true" } + { name: 'display-typeinfo', type: Boolean, defaultValue: false, description: "Display typeinfo of pairs of instruction orders and types when enable-typeinfo is true" }, + { name: 'function-sourcecode', type: Boolean, defaultValue: false, description: "Record functions' sourceCode to support the feature of [function].toString()" } ] @@ -275,6 +276,13 @@ export class CmdOptions { return this.options["debug-type"]; } + static needRecordSourceCode(): boolean { + if (!this.options) { + return false; + } + return this.options["function-sourcecode"]; + } + // @ts-ignore static parseUserCmd(args: string[]): ts.ParsedCommandLine | undefined { this.options = commandLineArgs(ts2pandaOptions, { partial: true }); diff --git a/ts2panda/src/compilerDriver.ts b/ts2panda/src/compilerDriver.ts index a5b3543ef547b6601821157176bd915acc119bd4..66747fbe58bf73dd6c64988d265ee005de8509d1 100644 --- a/ts2panda/src/compilerDriver.ts +++ b/ts2panda/src/compilerDriver.ts @@ -216,6 +216,11 @@ export class CompilerDriver { private compileImpl(node: ts.SourceFile | ts.FunctionLikeDeclaration, scope: Scope, internalName: string, recorder: Recorder): void { let pandaGen = new PandaGen(internalName, this.getParametersCount(node), scope); + + if (CmdOptions.needRecordSourceCode() && !ts.isSourceFile(node)) { + // souceCode of [ts.sourceFile] will be record in debugInfo later. + pandaGen.setSourceCode(node.getText()); + } // for debug info DebugInfo.addDebugIns(scope, pandaGen, true); @@ -262,6 +267,9 @@ export class CompilerDriver { private compileUnitTestImpl(node: ts.SourceFile | ts.FunctionLikeDeclaration, scope: Scope, internalName: string, recorder: Recorder) { let pandaGen = new PandaGen(internalName, this.getParametersCount(node), scope); + if (CmdOptions.needRecordSourceCode() && !ts.isSourceFile(node)) { + pandaGen.setSourceCode(node.getText()); + } let compiler = new Compiler(node, pandaGen, this, recorder); hoisting(node, pandaGen, recorder, compiler); diff --git a/ts2panda/src/debuginfo.ts b/ts2panda/src/debuginfo.ts index 982b92334ac084034166091af7330d2fb5f7b812..3df1f37ba5ef3b468467115e98b2db9a4d791126 100644 --- a/ts2panda/src/debuginfo.ts +++ b/ts2panda/src/debuginfo.ts @@ -354,11 +354,8 @@ export class DebugInfo { let sourceFile = jshelpers.getSourceFileOfNode(node); pandaGen.setSourceFileDebugInfo(sourceFile.fileName); - if (CmdOptions.isDebugMode()) { - if (ts.isSourceFile(node)) { - pandaGen.setSourceCodeDebugInfo(node.text); - } - return; + if (CmdOptions.isDebugMode() && ts.isSourceFile(node)) { + pandaGen.setSourceCode(node.text); } } diff --git a/ts2panda/src/pandagen.ts b/ts2panda/src/pandagen.ts index 45016fe8836e447937c39122cb6ce5b5b2b1d118..f44f9db6d71410796bba96f5b4a5baa578192f0c 100644 --- a/ts2panda/src/pandagen.ts +++ b/ts2panda/src/pandagen.ts @@ -203,8 +203,8 @@ export class PandaGen { // for debug info private variableDebugInfoArray: VariableDebugInfo[] = []; private firstStmt: ts.Statement | undefined; - private sourceFileDebugInfo: string = ""; - private sourceCodeDebugInfo: string | undefined; + private sourceFile: string = ""; + private sourceCode: string | undefined = undefined; private callType: number = 0; private static literalArrayBuffer: Array = new Array(); @@ -259,20 +259,20 @@ export class PandaGen { } } - public getSourceCodeDebugInfo() { - return this.sourceCodeDebugInfo; + public getSourceCode(): string | undefined { + return this.sourceCode; } - public setSourceCodeDebugInfo(code: string) { - this.sourceCodeDebugInfo = code; + public setSourceCode(code: string) { + this.sourceCode = code; } public getSourceFileDebugInfo() { - return this.sourceFileDebugInfo; + return this.sourceFile; } public setSourceFileDebugInfo(sourceFile: string) { - this.sourceFileDebugInfo = sourceFile; + this.sourceFile = sourceFile; } static getLiteralArrayBuffer() { diff --git a/ts2panda/src/ts2panda.ts b/ts2panda/src/ts2panda.ts index 39c62abb7aad19b20d68123d04af339b8e22db65..2bd1bc846d8617f1ca8508acb914b01d9fa421e8 100644 --- a/ts2panda/src/ts2panda.ts +++ b/ts2panda/src/ts2panda.ts @@ -254,13 +254,14 @@ export class Ts2Panda { ); } - let variables, sourceCode; + let variables = undefined, sourceCode = undefined; + if (CmdOptions.needRecordSourceCode() || CmdOptions.isDebugMode()) { + // function's sourceCode will be undefined in debugMode + // if we don't need to record function-sourceCode + sourceCode = pg.getSourceCode(); + } if (CmdOptions.isDebugMode()) { variables = pg.getVariableDebugInfoArray(); - sourceCode = pg.getSourceCodeDebugInfo(); - } else { - variables = undefined; - sourceCode = undefined; } let catchTableArr; diff --git a/ts2panda/tests/builtIns.test.ts b/ts2panda/tests/builtIns.test.ts index 2484fba12f72d056f9d7f8f8cfd60b2428e128f4..ace7fc1c21db2e0dc2a952c00e3363df2d1b622a 100644 --- a/ts2panda/tests/builtIns.test.ts +++ b/ts2panda/tests/builtIns.test.ts @@ -22,7 +22,8 @@ import { LdaDyn, VReg } from "../src/irnodes"; -import { checkInstructions, compileMainSnippet } from "./utils/base"; +import { checkInstructions, compileMainSnippet, SnippetCompiler } from "./utils/base"; +import { CmdOptions } from '../src/cmdOptions'; describe("BuiltInsTest", function () { it("Global Value Properties", function () { @@ -37,3 +38,15 @@ describe("BuiltInsTest", function () { expect(checkInstructions(insns, expected)).to.be.true; }); }); + +describe("FunctionToStringTest", function () { + it("func.toString()", function () { + CmdOptions.needRecordSourceCode = () => {return true}; + let snippetCompiler = new SnippetCompiler(); + snippetCompiler.compileAfter(`function foo() {return 123;}\nfunction bar() {return 321;}\n`, 'toStringTest.js'); + CmdOptions.needRecordSourceCode = () => {return false}; + let pandaGen = snippetCompiler.getPandaGenByName('foo'); + let expected = "function foo() {return 123;}"; + expect(pandaGen.getSourceCode() == expected).to.be.true; + }) +}) diff --git a/ts2panda/ts2abc/ts2abc.cpp b/ts2panda/ts2abc/ts2abc.cpp index 5f79472eaa201bda3dec39c404d4ada15249b2ef..f58511157852b205f1ffa40e2ea4be07c6438989 100644 --- a/ts2panda/ts2abc/ts2abc.cpp +++ b/ts2panda/ts2abc/ts2abc.cpp @@ -477,16 +477,14 @@ static int ParseVariablesDebugInfo(const Json::Value &function, panda::pandasm:: return RETURN_SUCCESS; } -static int ParseSourceFileDebugInfo(const Json::Value &function, panda::pandasm::Function &pandaFunc) +static int ParseSourceFileInfo(const Json::Value &function, panda::pandasm::Function &pandaFunc) { if (function.isMember("sf") && function["sf"].isString()) { pandaFunc.source_file = function["sf"].asString(); } - if (GetDebugModeEnabled()) { - if (function.isMember("sc") && function["sc"].isString()) { - pandaFunc.source_code = function["sc"].asString(); - } + if (function.isMember("sc") && function["sc"].isString()) { + pandaFunc.source_code = function["sc"].asString(); } return RETURN_SUCCESS; @@ -770,7 +768,7 @@ static panda::pandasm::Function ParseFunction(const Json::Value &function) auto pandaFunc = GetFunctionDefintion(function); ParseFunctionInstructions(function, pandaFunc); ParseVariablesDebugInfo(function, pandaFunc); - ParseSourceFileDebugInfo(function, pandaFunc); + ParseSourceFileInfo(function, pandaFunc); ParseFunctionLabels(function, pandaFunc); ParseFunctionCatchTables(function, pandaFunc); // parsing call opt type