use of com.eclipsesource.v8.V8 in project AndroidStudy by tinggengyan.
the class ReturnValueActivity method onCreate.
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_button);
this.textView = (TextView) findViewById(R.id.textView);
V8 runtime = V8.createV8Runtime();
String script = "" + "var hello = 'hello, ';\n" + "var world = 'world!';\n" + "hello.concat(world).length;\n";
int result = runtime.executeIntScript(script);
this.textView.setText("" + result);
runtime.release();
}
use of com.eclipsesource.v8.V8 in project rxlib by RockyLOMO.
the class JsonMapper method convert.
public <F, T> T convert(F from, Class<T> toType) {
require(from, toType);
Class fType = from.getClass();
Map<String, Object> map = getMap(fType, toType);
if (map == null) {
return JSON.parseObject(JSON.toJSONString(from), toType);
}
Object val = JSON.toJSON(from);
JSONObject json = as(val, JSONObject.class);
if (json == null) {
String jsonStr = (String) map.getOrDefault(String.valueOf(val), map.get(DefaultValue));
if (toType.isEnum()) {
Enum name = NQuery.of(toType.getEnumConstants()).<Enum>cast().where(p -> p.name().equals(jsonStr)).firstOrDefault();
if (name == null) {
return null;
}
return (T) name;
}
return JSON.parseObject(jsonStr, toType);
}
V8 v8 = runtime.getValue();
v8.add("_x", json.toJSONString());
String result = v8.executeStringScript(String.format(ScriptFunc, map.get("script")));
// System.out.println("result:" + result);
json.putAll(JSON.parseObject(result));
return json.toJavaObject(toType);
}
use of com.eclipsesource.v8.V8 in project mdw-designer by CenturyLinkCloud.
the class NodeRunner method run.
public void run(final TestCase testCase, DesignerDataAccess dao, boolean verbose, boolean createReplace) throws ServiceException {
PackageVO pkgVo = null;
NodeJS nodeJS = NodeJS.createNodeJS();
try {
pkgVo = dao.getPackage(NODE_PKG);
} catch (DataAccessException e) {
throw new ServiceException(e.getMessage());
}
final V8Object fileObj = new V8Object(nodeJS.getRuntime()).add("file", pkgVo.getRuleSet(RUNNER).getRawFile().getAbsolutePath());
JavaCallback callback = new JavaCallback() {
public Object invoke(V8Object receiver, V8Array parameters) {
return fileObj;
}
};
nodeJS.getRuntime().registerJavaMethod(callback, "getRunner");
final Result parseResult = new Result();
callback = new JavaCallback() {
public Object invoke(V8Object receiver, V8Array parameters) {
V8Object resultObj = parameters.getObject(0);
parseResult.status = resultObj.getString("status");
parseResult.message = resultObj.getString("message");
resultObj.release();
return null;
}
};
nodeJS.getRuntime().registerJavaMethod(callback, "setParseResult");
nodeJS.exec(pkgVo.getRuleSet(PARSER).getRawFile());
while (nodeJS.isRunning()) {
nodeJS.handleMessage();
}
fileObj.release();
nodeJS.release();
if (!parseResult.status.equals("OK"))
throw new ServiceException(PARSER + parseResult);
nodeJS = NodeJS.createNodeJS();
final V8Object testObj = new V8Object(nodeJS.getRuntime());
testObj.add("file", testCase.getCaseFile().getAbsolutePath());
V8Array valueFiles = new V8Array(nodeJS.getRuntime());
// TODO hardcoded
valueFiles.push("localhost.env");
testObj.add("valueFiles", valueFiles);
valueFiles.release();
testObj.add("resultDir", testCase.getResultDirectory().getAbsolutePath());
final Map<String, TestCaseItem> testCaseItems = new HashMap<>();
final V8Array testItems = new V8Array(nodeJS.getRuntime());
for (TestCaseItem item : testCase.getItems()) {
String itemId = item.getName();
V8Object itemObj = new V8Object(nodeJS.getRuntime()).add("name", item.getName());
try {
if (item.getObject().has("request")) {
JSONObject request = item.getObject().getJSONObject("request");
if (request.has("method")) {
itemObj.add("method", request.getString("method"));
itemId = request.getString("method") + ":" + itemId;
}
}
JSONObject options = item.getOptions() == null ? new JSONObject() : item.getOptions();
if (verbose && !options.has("debug"))
options.put("debug", "true");
if (createReplace && !options.has("overwriteExpected"))
options.put("overwriteExpected", "true");
options.put("qualifyLocations", false);
if (JSONObject.getNames(options) != null) {
V8Object json = nodeJS.getRuntime().getObject("JSON");
V8Array params = new V8Array(nodeJS.getRuntime()).push(options.toString());
V8Object jsonObj = json.executeObjectFunction("parse", params);
itemObj.add("options", jsonObj);
params.release();
json.release();
jsonObj.release();
}
if (item.getValues() != null) {
V8Object json = nodeJS.getRuntime().getObject("JSON");
V8Array params = new V8Array(nodeJS.getRuntime()).push(item.getValues().toString());
V8Object jsonObj = json.executeObjectFunction("parse", params);
itemObj.add("values", jsonObj);
params.release();
json.release();
jsonObj.release();
}
} catch (JSONException e) {
e.printStackTrace();
}
testItems.push(itemObj);
testCaseItems.put(itemId, item);
itemObj.release();
}
testObj.add("items", testItems);
testItems.release();
callback = new JavaCallback() {
public Object invoke(V8Object receiver, V8Array parameters) {
return testObj;
}
};
nodeJS.getRuntime().registerJavaMethod(callback, "getTestCase");
callback = new JavaCallback() {
public Object invoke(V8Object receiver, V8Array parameters) {
String itemId = parameters.getString(0);
V8Object resultObj = parameters.getObject(1);
if (itemId == null) {
for (TestCaseItem item : testCase.getItems()) {
updateItem(item, resultObj);
}
}
TestCaseItem item = testCaseItems.get(itemId);
if (item != null) {
updateItem(item, resultObj);
}
resultObj.release();
return null;
}
};
nodeJS.getRuntime().registerJavaMethod(callback, "setTestResult");
final V8 v8 = nodeJS.getRuntime();
callback = new JavaCallback() {
public Object invoke(V8Object receiver, V8Array parameters) {
String itemId = parameters.getString(0);
V8Object responseObj = parameters.getObject(1);
TestCaseItem item = testCaseItems.get(itemId);
if (item != null) {
V8Object json = v8.getObject("JSON");
V8Array params = new V8Array(v8).push(responseObj);
String jsonStr = json.executeStringFunction("stringify", params);
params.release();
json.release();
try {
item.setResponse(new JSONObject(jsonStr));
} catch (JSONException e) {
e.printStackTrace();
}
}
responseObj.release();
return null;
}
};
nodeJS.getRuntime().registerJavaMethod(callback, "setTestResponse");
nodeJS.exec(pkgVo.getRuleSet(RUNNER).getRawFile());
while (nodeJS.isRunning()) {
nodeJS.handleMessage();
}
testObj.release();
nodeJS.release();
}
Aggregations