use of com.dukescript.presenters.renderer.JSC in project dukescript-presenters by dukescript.
the class WebKitPresenter method protect.
private void protect(Object obj, Pointer pointer) {
JSC jsc = shell.jsc();
jsc.JSValueProtect(ctx, pointer);
ALL.add(new Protector(obj, pointer));
cleanProtected();
}
use of com.dukescript.presenters.renderer.JSC in project dukescript-presenters by dukescript.
the class WebKitPresenter method defineFn.
@Override
public Fn defineFn(String code, String[] names, boolean[] keepAlive) {
JSC jsc = shell.jsc();
Pointer[] jsNames = new Pointer[names.length];
for (int i = 0; i < jsNames.length; i++) {
jsNames[i] = jsc.JSStringCreateWithUTF8CString(names[i]);
}
Pointer jsCode = jsc.JSStringCreateWithUTF8CString(code);
PointerByReference exc = new PointerByReference();
Pointer fn = jsc.JSObjectMakeFunction(ctx, null, names.length, jsNames, jsCode, null, 1, exc);
if (fn == null) {
throw new IllegalStateException("Cannot initialize function: " + exc.getValue());
}
jsc.JSStringRelease(jsCode);
for (Pointer jsName : jsNames) {
jsc.JSStringRelease(jsName);
}
return new JSCFn(fn, keepAlive);
}
use of com.dukescript.presenters.renderer.JSC in project dukescript-presenters by dukescript.
the class WebKitPresenter method convertFromJava.
Pointer[] convertFromJava(Object[] args, boolean[] keepAlive) throws Exception {
JSC jsc = shell.jsc();
Pointer[] arr = new Pointer[args.length];
for (int i = 0; i < arr.length; i++) {
Object v = args[i];
if (v == null) {
v = jsc.JSValueMakeNull(ctx);
} else if (v instanceof Number) {
v = jsc.JSValueMakeNumber(ctx, ((Number) v).doubleValue());
} else if (v instanceof Boolean) {
v = ((Boolean) v) ? valueTrue : valueFalse;
} else if (v instanceof String) {
Pointer str = jsc.JSStringCreateWithUTF8CString((String) v);
v = jsc.JSValueMakeString(ctx, str);
jsc.JSStringRelease(str);
} else if (v instanceof Enum) {
Pointer str = jsc.JSStringCreateWithUTF8CString(((Enum) v).name());
v = jsc.JSValueMakeString(ctx, str);
jsc.JSStringRelease(str);
} else if (v instanceof Character) {
v = jsc.JSValueMakeNumber(ctx, (Character) v);
} else if (v instanceof JSObject) {
v = ((JSObject) v).value;
} else if (v instanceof int[]) {
int[] numbers = (int[]) v;
Pointer[] content = new Pointer[numbers.length];
for (int j = 0; j < content.length; j++) {
content[j] = jsc.JSValueMakeNumber(ctx, numbers[j]);
}
v = jsc.JSObjectMakeArray(ctx, content.length, content, null);
} else if (v instanceof double[]) {
double[] numbers = (double[]) v;
Pointer[] content = new Pointer[numbers.length];
for (int j = 0; j < content.length; j++) {
content[j] = jsc.JSValueMakeNumber(ctx, numbers[j]);
}
v = jsc.JSObjectMakeArray(ctx, content.length, content, null);
} else if (v instanceof Object[]) {
Pointer[] content = convertFromJava((Object[]) v);
v = jsc.JSObjectMakeArray(ctx, content.length, content, null);
} else if (v.getClass().isArray()) {
int len = Array.getLength(v);
Object[] boxed = new Object[len];
for (int j = 0; j < len; j++) {
boxed[j] = Array.get(v, j);
}
Pointer[] content = convertFromJava(boxed);
v = jsc.JSObjectMakeArray(ctx, content.length, content, null);
} else if (v.getClass().getSimpleName().equals("$JsCallbacks$")) {
Pointer vm = jsc.JSObjectMake(ctx, null, null);
for (Method method : v.getClass().getMethods()) {
if (method.getDeclaringClass() != v.getClass()) {
continue;
}
Pointer name = jsc.JSStringCreateWithUTF8CString(method.getName());
FnCallback fnC = new FnCallback(v, method);
toJava.put(fnC, fnC);
Pointer fn = jsc.JSObjectMakeFunctionWithCallback(ctx, null, fnC);
jsc.JSObjectSetProperty(ctx, vm, name, fn, 0, null);
jsc.JSStringRelease(name);
}
v = vm;
} else {
Pointer p = jsc.JSObjectMake(ctx, javaClazz, v);
if (keepAlive == null || keepAlive[i]) {
toJava.put(p, v);
} else {
toJava.put(p, new WeakVal(v));
}
protect(v, p);
v = p;
}
arr[i] = (Pointer) v;
}
return arr;
}
use of com.dukescript.presenters.renderer.JSC in project dukescript-presenters by dukescript.
the class WebKitPresenter method jsContext.
final void jsContext(Pointer ctx) {
this.ctx = ctx;
JSC jsc = shell.jsc();
onFinalize = new WebKitPresenter.OnFinalize();
javaClazz = jsc.JSClassCreate(new JSC.JSClassDefinition(onFinalize));
boolean testInstance = false;
assert testInstance = true;
if (testInstance) {
Pointer testObj = jsc.JSObjectMake(ctx, javaClazz, null);
assert isJavaClazz(testObj) : "Own classes has to be recognized";
}
{
Pointer jsGlobal = ctx;
Pointer arrArg = jsc.JSStringCreateWithUTF8CString("x");
Pointer arrT = jsc.JSStringCreateWithUTF8CString("var res = x.constructor === Array ? x.length : -1; return res;");
Pointer arrFn = jsc.JSObjectMakeFunction(jsGlobal, null, 1, new Pointer[] { arrArg }, arrT, null, 0, null);
arrayLength = arrFn;
jsc.JSValueProtect(ctx, arrFn);
assert !isJavaClazz(arrayLength) : "functions aren't Java classes";
}
{
Pointer trueScr = jsc.JSStringCreateWithUTF8CString("true");
valueTrue = jsc.JSEvaluateScript(ctx, trueScr, null, null, 1, null);
jsc.JSStringRelease(trueScr);
jsc.JSValueProtect(ctx, valueTrue);
int vT = jsc.JSValueGetType(ctx, valueTrue);
assert vT == 2;
assert !isJavaClazz(valueTrue) : "true isn't Java class";
}
{
Pointer falseScr = jsc.JSStringCreateWithUTF8CString("false");
valueFalse = jsc.JSEvaluateScript(ctx, falseScr, null, null, 1, null);
jsc.JSValueProtect(ctx, valueFalse);
jsc.JSStringRelease(falseScr);
int vF = jsc.JSValueGetType(ctx, valueFalse);
assert vF == 2;
assert !isJavaClazz(valueFalse) : "false isn't Java class";
}
}
Aggregations