diff --git a/scripts/node/jaspiler/index.d.ts b/scripts/node/jaspiler/index.d.ts index 3fe9390..daff761 100644 --- a/scripts/node/jaspiler/index.d.ts +++ b/scripts/node/jaspiler/index.d.ts @@ -759,6 +759,8 @@ interface TransformResult { } declare namespace jaspiler { + argv: Array; + function createCharacter(value: string): JTCharacter; function createFieldAccess(...values: string[]): JTFieldAccess; function createFloat(value: string): JTFloat; diff --git a/scripts/node/test/test_argv.js b/scripts/node/test/test_argv.js new file mode 100644 index 0000000..0751867 --- /dev/null +++ b/scripts/node/test/test_argv.js @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2023. caoccao.com Sam Cao + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +const { assert } = require('chai'); + +const argv = jaspiler.argv; + +assert.equal(4, argv.length); +assert.include(argv[0], 'test_argv.js'); +assert.equal('a', argv[1]); +assert.equal('b', argv[2]); +assert.equal('c', argv[3]); diff --git a/scripts/node/test/test_transform.js b/scripts/node/test/test_transform.js index 3102366..b3ef8d9 100644 --- a/scripts/node/test/test_transform.js +++ b/scripts/node/test/test_transform.js @@ -16,7 +16,7 @@ /// -const assert = require('chai').assert; +const { assert } = require('chai'); const path = require('path'); const process = require('process'); const { JTKind, PluginContractIgnore, PluginContractChangeMethod } = require('./jaspiler/jaspiler'); diff --git a/src/main/java/com/caoccao/jaspiler/JaspilerMain.java b/src/main/java/com/caoccao/jaspiler/JaspilerMain.java index c463791..42d9d42 100644 --- a/src/main/java/com/caoccao/jaspiler/JaspilerMain.java +++ b/src/main/java/com/caoccao/jaspiler/JaspilerMain.java @@ -26,6 +26,8 @@ import picocli.CommandLine; import java.io.File; +import java.util.ArrayList; +import java.util.List; import java.util.concurrent.Callable; @CommandLine.Command( @@ -34,11 +36,14 @@ version = JaspilerContract.VERSION, description = JaspilerContract.DESCRIPTION) public final class JaspilerMain extends BaseLoggingObject implements Callable { + @CommandLine.Parameters(hidden = true) + private List argv; @CommandLine.Parameters(index = "0", description = "The JavaScript file to be executed.") private File file; public JaspilerMain() { super(); + argv = new ArrayList<>(); file = null; } @@ -62,7 +67,7 @@ public Integer call() throws Exception { try (NodeRuntime nodeRuntime = V8Host.getNodeInstance().createV8Runtime()) { var javetProxyConverter = new JavetProxyConverter(); nodeRuntime.setConverter(javetProxyConverter); - try (V8Jaspiler v8Jaspiler = new V8Jaspiler(nodeRuntime)) { + try (V8Jaspiler v8Jaspiler = new V8Jaspiler(argv, nodeRuntime)) { nodeRuntime.getGlobalObject().set(V8Jaspiler.NAME, v8Jaspiler); nodeRuntime.getExecutor(file).executeVoid(); } finally { diff --git a/src/main/java/com/caoccao/jaspiler/v8/V8Jaspiler.java b/src/main/java/com/caoccao/jaspiler/v8/V8Jaspiler.java index 60d4f8b..d81b528 100644 --- a/src/main/java/com/caoccao/jaspiler/v8/V8Jaspiler.java +++ b/src/main/java/com/caoccao/jaspiler/v8/V8Jaspiler.java @@ -44,7 +44,9 @@ import java.io.IOException; import java.text.MessageFormat; import java.util.HashMap; +import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.function.Supplier; public final class V8Jaspiler @@ -58,8 +60,9 @@ public final class V8Jaspiler private static final String FUNCTION_CREATE_LITERAL = "createLiteral"; private static final String FUNCTION_CREATE_NAME = "createName"; private static final String FUNCTION_TRANSFORM_SYNC = "transformSync"; - private static final String PROPERTIES_AST = "ast"; - private static final String PROPERTIES_CODE = "code"; + private static final String PROPERTY_ARGV = "argv"; + private static final String PROPERTY_AST = "ast"; + private static final String PROPERTY_CODE = "code"; private static final Map>> constructorMap; static { @@ -130,13 +133,15 @@ public final class V8Jaspiler constructorMap.put("newYield", JTYield::new); } + private final List argv; private final Map> creatorMap; private final V8Runtime v8Runtime; private JaspilerCompiler jaspilerCompiler; private Map> stringGetterMap; - public V8Jaspiler(V8Runtime v8Runtime) { + public V8Jaspiler(List argv, V8Runtime v8Runtime) { super(); + this.argv = List.copyOf(Objects.requireNonNull(argv)); creatorMap = new HashMap<>(); creatorMap.put(FUNCTION_CREATE_CHARACTER, this::createCharacter); creatorMap.put(FUNCTION_CREATE_FIELD_ACCESS, this::createFieldAccess); @@ -194,6 +199,10 @@ public V8Value createName(V8Value... v8Values) throws JavetException, JaspilerAr return v8Runtime.toV8Value(new JTName(value)); } + public List getArgv() { + return argv; + } + @Override public V8Runtime getV8Runtime() { return v8Runtime; @@ -210,6 +219,7 @@ public boolean isClosed() { stringGetterMap = new HashMap<>(); constructorMap.forEach((key, value) -> registerStringGetterFunction(key, v8Values -> v8Runtime.toV8Value(value.get()))); creatorMap.forEach(this::registerStringGetterFunction); + registerStringGetter(PROPERTY_ARGV, propertyName -> v8Runtime.toV8Value(getArgv())); } return stringGetterMap; } @@ -238,12 +248,12 @@ public V8Value transformSync(V8Value... v8Values) throws JavetException, Jaspile try (V8Scope v8Scope = v8Runtime.getV8Scope()) { var v8ValueObjectResult = v8Scope.createV8ValueObject(); if (v8JaspilerOptions.isAst()) { - v8ValueObjectResult.set(PROPERTIES_AST, compilationUnitTree); + v8ValueObjectResult.set(PROPERTY_AST, compilationUnitTree); } if (v8JaspilerOptions.isCode()) { var writer = new StandardStyleWriter(v8JaspilerOptions.getStyleOptions()); if (compilationUnitTree.serialize(writer)) { - v8ValueObjectResult.set(PROPERTIES_CODE, writer.toString()); + v8ValueObjectResult.set(PROPERTY_CODE, writer.toString()); } } v8Scope.setEscapable(); diff --git a/src/test/java/com/caoccao/jaspiler/TestJaspilerMain.java b/src/test/java/com/caoccao/jaspiler/TestJaspilerMain.java index 4cbae86..7d3c9fb 100644 --- a/src/test/java/com/caoccao/jaspiler/TestJaspilerMain.java +++ b/src/test/java/com/caoccao/jaspiler/TestJaspilerMain.java @@ -18,10 +18,21 @@ import com.caoccao.jaspiler.utils.SystemUtils; import org.junit.jupiter.api.Test; +import picocli.CommandLine; + +import java.util.List; import static org.junit.jupiter.api.Assertions.assertEquals; public class TestJaspilerMain { + @Test + public void testArgv() throws Exception { + String scriptPath = SystemUtils.INITIAL_WORKING_DIRECTORY.resolve("scripts/node/test/test_argv.js") + .toAbsolutePath().toFile().getAbsolutePath(); + List args = List.of(scriptPath, "a", "b", "c"); + assertEquals(0, new CommandLine(new JaspilerMain()).execute(args.toArray(String[]::new))); + } + @Test public void testTransform() throws Exception { var jaspilerMain = new JaspilerMain();