Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restyle Upgrade quickjs version to 2024-0214 #74

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,54 +1,53 @@
package com.whl.quickjs.wrapper;

import org.junit.Test;

import static org.junit.Assert.*;

import com.whl.quickjs.android.QuickJSLoader;
import org.junit.Test;

/**
* Created by Harlon Wang on 2021/10/12.
*/
public class QuickJSModuleTest {

@Test
public void testModule() {
QuickJSLoader.init();
QuickJSContext context = QuickJSContext.create();
context.setModuleLoader(new QuickJSContext.DefaultModuleLoader() {
@Override
public String getModuleStringCode(String moduleName) {
if (moduleName.equals("./a.js")) {
return "export var name = 'Jack';\n" +
"export var age = 18;";
}
return null;
}
});
context.getGlobalObject().setProperty("assertName", args -> {
assertEquals("Jack", args[0]);
return null;
});
context.getGlobalObject().setProperty("assertAge", args -> {
assertEquals(18, args[0]);
return null;
});
context.getGlobalObject().setProperty("assertNameUpdated", args -> {
JSObject ret = (JSObject) args[0];
assertEquals("TypeError: 'name' is read-only", ret.toString());
ret.release();
return null;
});
Object ret = context.evaluateModule("import {name, age} from './a.js';\n" +
"\n" +
"assertName(name);\n" +
"assertAge(age);\n" +
"new Promise((resolve, reject) => { name = 'Updated'; }).catch((res) => { assertNameUpdated(res); });");

assertEquals(ret.toString(), "[object Promise]");

context.destroy();
}


@Test
public void testModule() {
QuickJSLoader.init();
QuickJSContext context = QuickJSContext.create();
context.setModuleLoader(new QuickJSContext.DefaultModuleLoader() {
@Override
public String getModuleStringCode(String moduleName) {
if (moduleName.equals("./a.js")) {
return "export var name = 'Jack';\n"
+ "export var age = 18;";
}
return null;
}
});
context.getGlobalObject().setProperty("assertName", args -> {
assertEquals("Jack", args[0]);
return null;
});
context.getGlobalObject().setProperty("assertAge", args -> {
assertEquals(18, args[0]);
return null;
});
context.getGlobalObject().setProperty("assertNameUpdated", args -> {
JSObject ret = (JSObject)args[0];
assertEquals("TypeError: 'name' is read-only", ret.toString());
ret.release();
return null;
});
Object ret = context.evaluateModule(
"import {name, age} from './a.js';\n"
+ "\n"
+ "assertName(name);\n"
+ "assertAge(age);\n"
+ "new Promise((resolve, reject) => { name = 'Updated'; " +
"}).catch((res) => { assertNameUpdated(res); });");

assertEquals(ret.toString(), "[object Promise]");

context.destroy();
}
}
Loading