Search in sources :

Example 1 with V8

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();
}
Also used : V8(com.eclipsesource.v8.V8)

Example 2 with V8

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);
}
Also used : V8(com.eclipsesource.v8.V8) SneakyThrows(lombok.SneakyThrows) Contract.as(org.rx.Contract.as) Contract.require(org.rx.Contract.require) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) HashMap(java.util.HashMap) V8Object(com.eclipsesource.v8.V8Object) org.rx(org.rx) Slf4j(lombok.extern.slf4j.Slf4j) JSON(com.alibaba.fastjson.JSON) Paths(java.nio.file.Paths) Map(java.util.Map) JSONObject(com.alibaba.fastjson.JSONObject) Path(java.nio.file.Path) Collections(java.util.Collections) V8(com.eclipsesource.v8.V8) JSONObject(com.alibaba.fastjson.JSONObject) V8Object(com.eclipsesource.v8.V8Object) JSONObject(com.alibaba.fastjson.JSONObject)

Example 3 with V8

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();
}
Also used : V8(com.eclipsesource.v8.V8) PackageVO(com.centurylink.mdw.model.value.process.PackageVO) HashMap(java.util.HashMap) V8Object(com.eclipsesource.v8.V8Object) JavaCallback(com.eclipsesource.v8.JavaCallback) V8Array(com.eclipsesource.v8.V8Array) JSONException(org.json.JSONException) NodeJS(com.eclipsesource.v8.NodeJS) ServiceException(com.centurylink.mdw.common.service.ServiceException) JSONObject(org.json.JSONObject) V8Object(com.eclipsesource.v8.V8Object) JSONObject(org.json.JSONObject) DataAccessException(com.centurylink.mdw.common.exception.DataAccessException)

Aggregations

V8 (com.eclipsesource.v8.V8)3 V8Object (com.eclipsesource.v8.V8Object)2 HashMap (java.util.HashMap)2 JSON (com.alibaba.fastjson.JSON)1 JSONObject (com.alibaba.fastjson.JSONObject)1 DataAccessException (com.centurylink.mdw.common.exception.DataAccessException)1 ServiceException (com.centurylink.mdw.common.service.ServiceException)1 PackageVO (com.centurylink.mdw.model.value.process.PackageVO)1 JavaCallback (com.eclipsesource.v8.JavaCallback)1 NodeJS (com.eclipsesource.v8.NodeJS)1 V8Array (com.eclipsesource.v8.V8Array)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 Collections (java.util.Collections)1 Map (java.util.Map)1 SneakyThrows (lombok.SneakyThrows)1 Slf4j (lombok.extern.slf4j.Slf4j)1 JSONException (org.json.JSONException)1