From 42c804ee77ca0c3f0a781482b111d9400d0939bc Mon Sep 17 00:00:00 2001 From: zgy-ian Date: Mon, 27 Sep 2021 11:21:51 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9generate=5Fplugin?= =?UTF-8?q?=E4=B8=ADjava=E4=BB=A3=E7=A0=81string=E8=BF=87=E9=95=BF?= =?UTF-8?q?=E7=9A=84=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zgy-ian --- ts2panda/scripts/generate_plugin.py | 40 ++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/ts2panda/scripts/generate_plugin.py b/ts2panda/scripts/generate_plugin.py index 306e9696d2..1744ee6e26 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 -- Gitee From 7d3c6d3e87c5cbd536dc497aa3b83845aa10eb77 Mon Sep 17 00:00:00 2001 From: zgy-ian Date: Thu, 7 Oct 2021 16:18:31 +0800 Subject: [PATCH 2/2] Modify the transformation of tsc from the before phase to beafter Signed-off-by: zgy-ian --- ts2panda/src/compilerDriver.ts | 15 +++++++++++++++ ts2panda/src/index.ts | 28 ++++++++++++++++++++++++---- ts2panda/src/recorder.ts | 12 +++++++----- ts2panda/src/syntaxCheckHelper.ts | 4 ++++ 4 files changed, 50 insertions(+), 9 deletions(-) diff --git a/ts2panda/src/compilerDriver.ts b/ts2panda/src/compilerDriver.ts index e8fa59c596..1fa363f516 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 f778f0a75e..305fc316b7 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 d74afb9d1c..391b4b8c4c 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 2c719c38f4..8439b27263 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; -- Gitee