2021-11-05 11:59:54 +08:00
|
|
|
declare module NativeClient {
|
2021-11-05 15:14:32 +08:00
|
|
|
function log(message: string): void
|
|
|
|
function returnNative(ret: string): void
|
2021-11-05 11:59:54 +08:00
|
|
|
function callNative(name: string, args: string): string
|
|
|
|
}
|
|
|
|
|
|
|
|
type RawValue = number | string | boolean | object | undefined
|
|
|
|
|
|
|
|
type WrappedValue = {
|
|
|
|
type: "number" | "string" | "boolean" | "object" | "array" | "null",
|
|
|
|
value: RawValue,
|
|
|
|
}
|
2021-11-05 15:14:32 +08:00
|
|
|
function _binaryValue(v: RawValue) {
|
|
|
|
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
|
|
|
|
2021-11-05 15:14:32 +08:00
|
|
|
}
|
2021-11-05 11:59:54 +08:00
|
|
|
function _wrappedValue(v: RawValue): WrappedValue {
|
|
|
|
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: WrappedValue): RawValue {
|
|
|
|
switch (v.type) {
|
|
|
|
case "number":
|
2021-11-05 15:14:32 +08:00
|
|
|
return v.value;
|
2021-11-05 11:59:54 +08:00
|
|
|
case "string":
|
2021-11-05 15:14:32 +08:00
|
|
|
return v.value;
|
2021-11-05 11:59:54 +08:00
|
|
|
case "boolean":
|
2021-11-05 15:14:32 +08:00
|
|
|
return v.value;
|
2021-11-05 11:59:54 +08:00
|
|
|
case "object":
|
|
|
|
case "array":
|
2021-11-05 15:14:32 +08:00
|
|
|
if (typeof v.value === 'string') {
|
|
|
|
return JSON.parse(v.value)
|
|
|
|
}
|
|
|
|
return v.value;
|
2021-11-05 11:59:54 +08:00
|
|
|
default:
|
2021-11-05 15:14:32 +08:00
|
|
|
return undefined;
|
2021-11-05 11:59:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-05 16:31:38 +08:00
|
|
|
function __injectGlobalObject(name: string, args: string) {
|
2021-11-05 11:59:54 +08:00
|
|
|
Reflect.set(window, name, JSON.parse(args));
|
|
|
|
}
|
|
|
|
|
|
|
|
function __injectGlobalFunction(name: string) {
|
|
|
|
Reflect.set(window, name, function () {
|
|
|
|
const args: any[] = [];
|
|
|
|
for (let i = 0; i < arguments.length; i++) {
|
|
|
|
args.push(_wrappedValue(arguments[i]));
|
|
|
|
}
|
|
|
|
const ret = NativeClient.callNative(name, JSON.stringify(args));
|
2021-11-05 15:14:32 +08:00
|
|
|
return _rawValue(JSON.parse(ret));
|
2021-11-05 11:59:54 +08:00
|
|
|
});
|
2021-11-05 15:14:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function __invokeMethod(objectName: string, functionName: string, stringifiedArgs: string) {
|
2021-11-05 16:31:38 +08:00
|
|
|
// NativeClient.log(`invoke:${objectName}.${functionName}(${stringifiedArgs})`)
|
2021-11-05 15:14:32 +08:00
|
|
|
try {
|
|
|
|
const thisObject = Reflect.get(window, objectName);
|
|
|
|
const thisFunction = Reflect.get(thisObject, functionName);
|
|
|
|
const args = JSON.parse(stringifiedArgs) as WrappedValue[];
|
|
|
|
const rawArgs = args.map(e => _rawValue(e));
|
|
|
|
const ret = Reflect.apply(thisFunction, thisObject, rawArgs);
|
2021-11-05 15:40:25 +08:00
|
|
|
const returnVal = ret ? JSON.stringify(_wrappedValue(ret)) : ""
|
2021-11-05 16:31:38 +08:00
|
|
|
// NativeClient.log(`return:${returnVal}`)
|
2021-11-05 15:14:32 +08:00
|
|
|
NativeClient.returnNative(returnVal)
|
|
|
|
} catch (e) {
|
|
|
|
NativeClient.log(`error:${e},${(e as any).stack}`)
|
|
|
|
NativeClient.returnNative("")
|
|
|
|
}
|
2021-11-05 15:40:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function _prepared() {
|
|
|
|
window.setTimeout = Reflect.get(window, "doricSetTimeout");
|
|
|
|
window.setInterval = Reflect.get(window, "doricSetInterval");
|
|
|
|
window.clearTimeout = Reflect.get(window, "doricClearTimeout");
|
|
|
|
window.clearInterval = Reflect.get(window, "doricClearInterval");
|
2021-11-05 11:59:54 +08:00
|
|
|
}
|