Skip to content

Commit

Permalink
🦄 refactor: Add V8PatchedFileExecutor
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Jun 30, 2023
1 parent 121d057 commit 380334c
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 5 deletions.
2 changes: 1 addition & 1 deletion scripts/node/test/test_transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
const { assert } = require('chai');
const path = require('path');
const process = require('process');
const { JTKind, PluginContractIgnore, PluginContractChangeMethod } = require('./jaspiler/jaspiler');
const { JTKind, PluginContractIgnore, PluginContractChangeMethod } = require('../jaspiler/jaspiler');

const workingDirectory = process.cwd();
const pathMockAllInOnePublicClass = path.join(
Expand Down
2 changes: 1 addition & 1 deletion scripts/node/tutorials/02_play_with_types.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

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

const { JTKind } = require('./jaspiler/jaspiler');
const { JTKind } = require('../jaspiler/jaspiler');

const result = jaspiler.transformSync(
`package com.test;
Expand Down
2 changes: 1 addition & 1 deletion scripts/node/tutorials/03_builtin_annotations.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

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

const { PluginContractIgnore, PluginContractChangeMethod } = require('./jaspiler/jaspiler');
const { PluginContractIgnore, PluginContractChangeMethod } = require('../jaspiler/jaspiler');

const result = jaspiler.transformSync(
`package com.test;
Expand Down
2 changes: 1 addition & 1 deletion scripts/node/tutorials/04_parse_argv.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/// <reference types="../jaspiler/index.d.ts"/>

process.argv = [process.argv[0], ...jaspiler.argv];

process.chdir('../')
const yargs = require('yargs');

const argv = yargs
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/caoccao/jaspiler/JaspilerMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.caoccao.jaspiler.enums.JaspilerExitCode;
import com.caoccao.jaspiler.utils.BaseLoggingObject;
import com.caoccao.jaspiler.v8.V8Jaspiler;
import com.caoccao.jaspiler.v8.V8PatchedFileExecutor;
import com.caoccao.javet.exceptions.JavetException;
import com.caoccao.javet.interop.NodeRuntime;
import com.caoccao.javet.interop.V8Host;
Expand Down Expand Up @@ -52,7 +53,8 @@ public JaspilerExitCode execute(String[] args) {
nodeRuntime.setConverter(javetProxyConverter);
try (V8Jaspiler v8Jaspiler = new V8Jaspiler(args, nodeRuntime)) {
nodeRuntime.getGlobalObject().set(V8Jaspiler.NAME, v8Jaspiler);
nodeRuntime.getExecutor(file).executeVoid();
var executor = new V8PatchedFileExecutor(nodeRuntime, file);
executor.executeVoid();
} finally {
nodeRuntime.getGlobalObject().delete(V8Jaspiler.NAME);
nodeRuntime.lowMemoryNotification();
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/com/caoccao/jaspiler/v8/V8PatchedFileExecutor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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.
*/

package com.caoccao.jaspiler.v8;

import com.caoccao.javet.exceptions.JavetException;
import com.caoccao.javet.interop.NodeRuntime;
import com.caoccao.javet.interop.V8Runtime;
import com.caoccao.javet.interop.executors.IV8Executor;
import com.caoccao.javet.interop.executors.V8FileExecutor;
import com.caoccao.javet.node.modules.NodeModuleModule;
import com.caoccao.javet.node.modules.NodeModuleProcess;

import java.io.File;

public class V8PatchedFileExecutor extends V8FileExecutor {
public V8PatchedFileExecutor(V8Runtime v8Runtime, File scriptFile) throws JavetException {
super(v8Runtime, scriptFile);
}

@Override
public IV8Executor setResourceName(String resourceName) throws JavetException {
getV8ScriptOrigin().setResourceName(resourceName);
V8Runtime v8Runtime = getV8Runtime();
if (v8Runtime.getJSRuntimeType().isNode()) {
NodeRuntime nodeRuntime = (NodeRuntime) v8Runtime;
File resourceFile = new File(resourceName);
File parentFile = resourceFile.getParentFile();
nodeRuntime.getGlobalObject().set(NodeRuntime.PROPERTY_DIRNAME, parentFile.getAbsolutePath());
nodeRuntime.getGlobalObject().set(NodeRuntime.PROPERTY_FILENAME, resourceFile.getAbsolutePath());
nodeRuntime.getNodeModule(NodeModuleModule.class).setRequireRootDirectory(resourceFile.getAbsolutePath());
nodeRuntime.getNodeModule(NodeModuleProcess.class).setWorkingDirectory(parentFile.getAbsolutePath());
}
return this;
}
}

0 comments on commit 380334c

Please sign in to comment.