Skip to content

Commit

Permalink
✨ feat: Add jaspiler.argv
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Jun 29, 2023
1 parent 678edb4 commit 16e875a
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 7 deletions.
2 changes: 2 additions & 0 deletions scripts/node/jaspiler/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,8 @@ interface TransformResult {
}

declare namespace jaspiler {
argv: Array<string>;

function createCharacter(value: string): JTCharacter;
function createFieldAccess(...values: string[]): JTFieldAccess;
function createFloat(value: string): JTFloat;
Expand Down
25 changes: 25 additions & 0 deletions scripts/node/test/test_argv.js
Original file line number Diff line number Diff line change
@@ -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]);
2 changes: 1 addition & 1 deletion scripts/node/test/test_transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

/// <reference types="../jaspiler/index.d.ts"/>

const assert = require('chai').assert;
const { assert } = require('chai');
const path = require('path');
const process = require('process');
const { JTKind, PluginContractIgnore, PluginContractChangeMethod } = require('./jaspiler/jaspiler');
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/com/caoccao/jaspiler/JaspilerMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -34,11 +36,14 @@
version = JaspilerContract.VERSION,
description = JaspilerContract.DESCRIPTION)
public final class JaspilerMain extends BaseLoggingObject implements Callable<Integer> {
@CommandLine.Parameters(hidden = true)
private List<String> argv;
@CommandLine.Parameters(index = "0", description = "The JavaScript file to be executed.")
private File file;

public JaspilerMain() {
super();
argv = new ArrayList<>();
file = null;
}

Expand All @@ -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 {
Expand Down
20 changes: 15 additions & 5 deletions src/main/java/com/caoccao/jaspiler/v8/V8Jaspiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<String, Supplier<JTTree<?, ?>>> constructorMap;

static {
Expand Down Expand Up @@ -130,13 +133,15 @@ public final class V8Jaspiler
constructorMap.put("newYield", JTYield::new);
}

private final List<String> argv;
private final Map<String, IJavetDirectCallable.NoThisAndResult<?>> creatorMap;
private final V8Runtime v8Runtime;
private JaspilerCompiler jaspilerCompiler;
private Map<String, IJavetUniFunction<String, ? extends V8Value, JaspilerCheckedException>> stringGetterMap;

public V8Jaspiler(V8Runtime v8Runtime) {
public V8Jaspiler(List<String> 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);
Expand Down Expand Up @@ -194,6 +199,10 @@ public V8Value createName(V8Value... v8Values) throws JavetException, JaspilerAr
return v8Runtime.toV8Value(new JTName(value));
}

public List<String> getArgv() {
return argv;
}

@Override
public V8Runtime getV8Runtime() {
return v8Runtime;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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();
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/com/caoccao/jaspiler/TestJaspilerMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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();
Expand Down

0 comments on commit 16e875a

Please sign in to comment.