2021-11-04 18:50:32 +08:00
|
|
|
"use strict";
|
2021-11-05 15:14:32 +08:00
|
|
|
function _binaryValue(v) {
|
|
|
|
switch (typeof v) {
|
|
|
|
case "number":
|
|
|
|
return {
|
|
|
|
type: "number",
|
|
|
|
value: v
|
|
|
|
};
|
|
|
|
case "string":
|
|
|
|
return {
|
|
|
|
type: "string",
|
|
|
|
value: v
|
|
|
|
};
|
|
|
|
case "boolean":
|
|
|
|
return {
|
|
|
|
type: "boolean",
|
|
|
|
value: v
|
|
|
|
};
|
|
|
|
case "object":
|
|
|
|
if (v instanceof Array) {
|
|
|
|
return {
|
|
|
|
type: "array",
|
|
|
|
value: JSON.stringify(v)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return {
|
|
|
|
type: "object",
|
|
|
|
value: JSON.stringify(v)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return {
|
|
|
|
type: "null",
|
|
|
|
value: undefined
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2021-11-05 11:59:54 +08:00
|
|
|
function _wrappedValue(v) {
|
|
|
|
switch (typeof v) {
|
|
|
|
case "number":
|
|
|
|
return {
|
|
|
|
type: "number",
|
|
|
|
value: v
|
|
|
|
};
|
|
|
|
case "string":
|
|
|
|
return {
|
|
|
|
type: "string",
|
|
|
|
value: v
|
|
|
|
};
|
|
|
|
case "boolean":
|
|
|
|
return {
|
|
|
|
type: "boolean",
|
|
|
|
value: v
|
|
|
|
};
|
|
|
|
case "object":
|
|
|
|
if (v instanceof Array) {
|
|
|
|
return {
|
|
|
|
type: "array",
|
|
|
|
value: JSON.stringify(v)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return {
|
|
|
|
type: "object",
|
|
|
|
value: JSON.stringify(v)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return {
|
|
|
|
type: "null",
|
|
|
|
value: undefined
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function _rawValue(v) {
|
|
|
|
switch (v.type) {
|
|
|
|
case "number":
|
|
|
|
return v.value;
|
|
|
|
case "string":
|
|
|
|
return v.value;
|
|
|
|
case "boolean":
|
|
|
|
return v.value;
|
|
|
|
case "object":
|
|
|
|
case "array":
|
2021-11-05 15:14:32 +08:00
|
|
|
return v.value;
|
2021-11-05 11:59:54 +08:00
|
|
|
default:
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
}
|
2021-11-05 15:14:32 +08:00
|
|
|
function _injectGlobalObject(name, args) {
|
2021-11-05 11:59:54 +08:00
|
|
|
Reflect.set(window, name, JSON.parse(args));
|
|
|
|
}
|
|
|
|
function __injectGlobalFunction(name) {
|
|
|
|
Reflect.set(window, name, function () {
|
|
|
|
const args = [];
|
|
|
|
for (let i = 0; i < arguments.length; i++) {
|
|
|
|
args.push(_wrappedValue(arguments[i]));
|
|
|
|
}
|
|
|
|
const ret = NativeClient.callNative(name, JSON.stringify(args));
|
|
|
|
return _rawValue(JSON.parse(ret));
|
|
|
|
});
|
|
|
|
}
|
2021-11-05 15:14:32 +08:00
|
|
|
function __invokeMethod(objectName, functionName, stringifiedArgs) {
|
|
|
|
NativeClient.log(`invoke:${objectName}.${functionName}(${stringifiedArgs})`);
|
|
|
|
try {
|
|
|
|
const thisObject = Reflect.get(window, objectName);
|
|
|
|
const thisFunction = Reflect.get(thisObject, functionName);
|
|
|
|
const args = JSON.parse(stringifiedArgs);
|
|
|
|
args.forEach(e => {
|
|
|
|
NativeClient.log(`Arg:${e},${typeof e}`);
|
|
|
|
});
|
|
|
|
const rawArgs = args.map(e => _rawValue(e));
|
|
|
|
rawArgs.forEach(e => {
|
|
|
|
NativeClient.log(`RawArg:${e},${typeof e}`);
|
|
|
|
});
|
|
|
|
const ret = Reflect.apply(thisFunction, thisObject, rawArgs);
|
|
|
|
const returnVal = JSON.stringify(_wrappedValue(ret));
|
|
|
|
NativeClient.log(`return:${returnVal}`);
|
|
|
|
NativeClient.returnNative(returnVal);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
NativeClient.log(`error:${e},${e.stack}`);
|
|
|
|
NativeClient.returnNative("");
|
|
|
|
}
|
|
|
|
}
|