diff --git a/ts2panda/scripts/generate_plugin.py b/ts2panda/scripts/generate_plugin.py index 306e9696d26223c0d8a8410728e56b1b30b586d7..1744ee6e26743d46b6d9cf885273d1d83b7b5bec 100755 --- a/ts2panda/scripts/generate_plugin.py +++ b/ts2panda/scripts/generate_plugin.py @@ -104,17 +104,39 @@ def gen_java_method(input_arguments): # write method: getJsCode with open(js_src_file, "r") as input_src: - output.write(" public static String getJsCode() {%s" - % os.linesep) - output.write(" return%s" % os.linesep) lines = input_src.readlines() - for line in lines[:-1]: + # seperate_lines into blocks + single_block_len = 1024 + total_len = len(lines) + for index, line in enumerate(lines): + block_index = index // single_block_len line = line.strip(os.linesep) line = line.replace("\"", "\\\"") - output.write(" \"%s\\n\" +%s" % (line, os.linesep)) - - last_line = lines[-1].replace("\"", "\\\"").strip(os.linesep) - output.write(" \"%s\";%s" % (last_line, os.linesep)) + # generate getJsCode%s + if (index % single_block_len == 0): + output.write(" private static String getJsCode%s(){%s" + % (block_index, os.linesep)) + output.write(" return%s" % os.linesep) + if (index % single_block_len == single_block_len-1 or index == total_len - 1): + output.write(" \"%s\";%s" % (line, os.linesep)) + output.write(" }%s" % os.linesep) + else: + output.write(" \"%s\\n\" +%s" % (line, os.linesep)) + block_num = (total_len//single_block_len) + 1 + if total_len % single_block_len == 0: + block_num = total_len // single_block_len + # generate getJsCode + output.write( + " public static String getJsCode(){%s" % os.linesep) + output.write(" return %s" % os.linesep) + # let getJsCode call getJsCode%s + for index in range(block_num): + if (index != block_num - 1): + output.write(" getJsCode%s() +%s" % + (index, os.linesep)) + else: + output.write(" getJsCode%s() ;%s" % + (index, os.linesep)) output.write(" }%s" % os.linesep) output.write("%s" % os.linesep) @@ -167,4 +189,4 @@ def operate_file(input_arguments): if __name__ == "__main__": - operate_file(parse_args()) + operate_file(parse_args()) \ No newline at end of file diff --git a/ts2panda/src/compilerDriver.ts b/ts2panda/src/compilerDriver.ts index e8fa59c59695974570ff6e896c80f7103e2c54b5..1fa363f51612a30c0b3841a3eea0586aa2eea41d 100644 --- a/ts2panda/src/compilerDriver.ts +++ b/ts2panda/src/compilerDriver.ts @@ -143,6 +143,21 @@ export class CompilerDriver { return spArray.reverse(); } + compileForSyntaxCheck(node: ts.SourceFile): void { + if (CmdOptions.showASTStatistics()) { + let statics: number[] = new Array(ts.SyntaxKind.Count).fill(0); + + this.getASTStatistics(node, statics); + statics.forEach((element, idx) => { + if (element > 0) { + LOGD(this.kind2String(idx) + " = " + element); + } + }); + } + + this.compilePrologue(node); + } + compile(node: ts.SourceFile): void { if (CmdOptions.showASTStatistics()) { let statics: number[] = new Array(ts.SyntaxKind.Count).fill(0); diff --git a/ts2panda/src/index.ts b/ts2panda/src/index.ts index f778f0a75e74d54f8c02c1aacde5dfda9e89ed7e..305fc316b777f44cd663ac51a80a5ffbe3005402 100644 --- a/ts2panda/src/index.ts +++ b/ts2panda/src/index.ts @@ -36,11 +36,22 @@ function main(fileNames: string[], options: ts.CompilerOptions) { before: [ (ctx: ts.TransformationContext) => { return (node: ts.SourceFile) => { - let outputBinName = CmdOptions.getOutputBinName(); - let fileName = node.fileName.substring(0, node.fileName.lastIndexOf('.')); - if (fileName != CmdOptions.getInputFileName()) { - outputBinName = fileName + ".abc"; + let outputBinName = getOutputBinName(node); + let compilerDriver = new CompilerDriver(outputBinName); + compilerDriver.compileForSyntaxCheck(node); + return node; + } + } + ], + after: [ + (ctx: ts.TransformationContext) => { + return (node: ts.SourceFile) => { + if (ts.getEmitHelpers(node)) { + const text: string = node.getText(); + let newNode = ts.createSourceFile(node.fileName, text, options.target!); + node = newNode; } + let outputBinName = getOutputBinName(node); let compilerDriver = new CompilerDriver(outputBinName); setGlobalStrict(jshelpers.isEffectiveStrictModeSourceFile(node, options)); if (CmdOptions.isVariantBytecode()) { @@ -70,6 +81,15 @@ function main(fileNames: string[], options: ts.CompilerOptions) { }); } +function getOutputBinName(node: ts.SourceFile) { + let outputBinName = CmdOptions.getOutputBinName(); + let fileName = node.fileName.substring(0, node.fileName.lastIndexOf('.')); + if (fileName != CmdOptions.getInputFileName()) { + outputBinName = fileName + ".abc"; + } + return outputBinName; +} + namespace Compiler { export namespace Options { export let Default: ts.CompilerOptions = { diff --git a/ts2panda/src/recorder.ts b/ts2panda/src/recorder.ts index d74afb9d1c3378feb0bff1e318a1e1610b83215a..391b4b8c4cd34312f35368c63c71c367952638c2 100644 --- a/ts2panda/src/recorder.ts +++ b/ts2panda/src/recorder.ts @@ -42,9 +42,7 @@ import { VariableScope } from "./scope"; import { - AddCtor2Class, - isContainConstruct, - getClassNameForConstructor + AddCtor2Class, getClassNameForConstructor, isContainConstruct } from "./statement/classStatement"; import { checkSyntaxError } from "./syntaxChecker"; import { isGlobalIdentifier } from "./syntaxCheckHelper"; @@ -83,6 +81,10 @@ export class Recorder { private recordInfo(node: ts.Node, scope: Scope) { node.forEachChild(childNode => { + if (childNode!.parent == undefined || childNode.parent.kind != node.kind) { + childNode = jshelpers.setParent(childNode, node)!; + childNode = ts.setTextRange(childNode, node)!; + } checkSyntaxError(childNode); switch (childNode.kind) { case ts.SyntaxKind.FunctionExpression: @@ -217,7 +219,7 @@ export class Recorder { let tmp: Scope | undefined = nearestRefVariableScope.getNearestLexicalScope(); let needCreateLoopEnv: boolean = false; if (nearestDefLexicalScope instanceof LoopScope) { - while(tmp) { + while (tmp) { if (tmp == nearestDefLexicalScope) { needCreateLoopEnv = true; break; @@ -236,7 +238,7 @@ export class Recorder { if (name == "arguments") { let varialbeScope = scope.getNearestVariableScope(); - varialbeScope ?.setUseArgs(true); + varialbeScope?.setUseArgs(true); } } diff --git a/ts2panda/src/syntaxCheckHelper.ts b/ts2panda/src/syntaxCheckHelper.ts index 2c719c38f4175b8691a4eb8a4f3f505d39aea41d..8439b27263c0053e12b7543c7300efa8f5c99d11 100644 --- a/ts2panda/src/syntaxCheckHelper.ts +++ b/ts2panda/src/syntaxCheckHelper.ts @@ -256,6 +256,10 @@ export function isOptionalParameter(node: ts.ParameterDeclaration): boolean { return true; } + if (!node.parent || !node.parent.parent) { + return false; + } + let iife = jshelpers.getImmediatelyInvokedFunctionExpression(node.parent); if (iife) { return !node.type && !node.dotDotDotToken && node.parent.parameters.indexOf(node) >= iife.arguments.length;