Skip to content

Commit

Permalink
wip(uts): runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
fxy060608 committed Mar 21, 2023
1 parent e78539b commit 1a9113a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 13 deletions.
3 changes: 2 additions & 1 deletion src/platforms/app-plus/service/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ export {
initUTSClassName,
initUTSPackageName,
requireUTSPlugin,
registerUTSPlugin
registerUTSPlugin,
registerUTSInterface
} from './plugin/uts'

export * from './route/navigate-back'
Expand Down
66 changes: 54 additions & 12 deletions src/platforms/app-plus/service/api/plugin/uts.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isPlainObject, hasOwn, extend, capitalize } from 'uni-shared';
import { isPlainObject, hasOwn, extend, capitalize, isString } from 'uni-shared';

let callbackId = 1;
let proxy;
Expand All @@ -18,22 +18,37 @@ function normalizeArg(arg) {
}
return arg;
}
function initUTSInstanceMethod(async, opts, instanceId) {
return initProxyFunction(async, opts, instanceId);
function initUTSInstanceMethod(async, opts, instanceId, proxy) {
return initProxyFunction(async, opts, instanceId, proxy);
}
function getProxy() {
if (!proxy) {
proxy = uni.requireNativePlugin('UTS-Proxy');
}
return proxy;
}
function resolveSyncResult(res) {
function resolveSyncResult(res, returnOptions, instanceId, proxy) {
// devtools 环境是字符串?
if (isString(res)) {
res = JSON.parse(res);
}
if ((process.env.NODE_ENV !== 'production')) {
console.log('uts.invokeSync.result', res);
console.log('uts.invokeSync.result', res, returnOptions, instanceId, proxy);
}
if (res.errMsg) {
throw new Error(res.errMsg);
}
if (returnOptions) {
if (returnOptions.type === 'interface' && typeof res.params === 'number') {
if (res.params === instanceId && proxy) {
return proxy;
}
if (interfaceDefines[returnOptions.options]) {
const ProxyClass = initUTSProxyClass(extend({ instanceId: res.params }, interfaceDefines[returnOptions.options]));
return new ProxyClass();
}
}
}
return res.params;
}
function invokePropGetter(args) {
Expand All @@ -46,7 +61,7 @@ function invokePropGetter(args) {
}
return resolveSyncResult(getProxy().invokeSync(args, () => { }));
}
function initProxyFunction(async, { moduleName, moduleType, package: pkg, class: cls, name: propOrMethod, method, companion, params: methodParams, errMsg, }, instanceId) {
function initProxyFunction(async, { moduleName, moduleType, package: pkg, class: cls, name: propOrMethod, method, companion, params: methodParams, return: returnOptions, errMsg, }, instanceId, proxy) {
const invokeCallback = ({ id, name, params, keepAlive, }) => {
const callback = callbacks[id];
if (callback) {
Expand Down Expand Up @@ -109,7 +124,7 @@ function initProxyFunction(async, { moduleName, moduleType, package: pkg, class:
if ((process.env.NODE_ENV !== 'production')) {
console.log('uts.invokeSync.args', invokeArgs);
}
return resolveSyncResult(getProxy().invokeSync(invokeArgs, invokeCallback));
return resolveSyncResult(getProxy().invokeSync(invokeArgs, invokeCallback), returnOptions, instanceId, proxy);
};
}
function initUTSStaticMethod(async, opts) {
Expand All @@ -127,14 +142,33 @@ function parseClassMethodName(name, methods) {
}
return name;
}
function initUTSProxyClass({ moduleName, moduleType, package: pkg, class: cls, constructor: { params: constructorParams }, methods, props, staticProps, staticMethods, errMsg, }) {
function isUndefined(value) {
return typeof value === 'undefined';
}
function isProxyInterfaceOptions(options) {
return !isUndefined(options.instanceId);
}
function initUTSProxyClass(options) {
const { moduleName, moduleType, package: pkg, class: cls, methods, props, errMsg, } = options;
const baseOptions = {
moduleName,
moduleType,
package: pkg,
class: cls,
errMsg,
};
let instanceId;
let constructorParams = [];
let staticMethods = {};
let staticProps = [];
if (isProxyInterfaceOptions(options)) {
instanceId = options.instanceId;
}
else {
constructorParams = options.constructor.params;
staticMethods = options.staticMethods;
staticProps = options.staticProps;
}
// iOS 需要为 ByJs 的 class 构造函数(如果包含JSONObject或UTSCallback类型)补充最后一个参数
if (typeof plus !== 'undefined' && plus.os.name === 'iOS') {
if (constructorParams.find((p) => p.type === 'UTSCallback' || p.type.indexOf('JSONObject') > 0)) {
Expand All @@ -148,11 +182,14 @@ function initUTSProxyClass({ moduleName, moduleType, package: pkg, class: cls, c
}
const target = {};
// 初始化实例 ID
const instanceId = initProxyFunction(false, extend({ name: 'constructor', params: constructorParams }, baseOptions), 0).apply(null, params);
if (isUndefined(instanceId)) {
// 未指定instanceId
instanceId = initProxyFunction(false, extend({ name: 'constructor', params: constructorParams }, baseOptions), 0).apply(null, params);
}
if (!instanceId) {
throw new Error(`new ${cls} is failed`);
}
return new Proxy(this, {
const proxy = new Proxy(this, {
get(_, name) {
if (!target[name]) {
//实例方法
Expand All @@ -162,7 +199,7 @@ function initUTSProxyClass({ moduleName, moduleType, package: pkg, class: cls, c
target[name] = initUTSInstanceMethod(!!async, extend({
name,
params,
}, baseOptions), instanceId);
}, baseOptions), instanceId, proxy);
}
else if (props.includes(name)) {
// 实例属性
Expand All @@ -178,6 +215,7 @@ function initUTSProxyClass({ moduleName, moduleType, package: pkg, class: cls, c
return target[name];
},
});
return proxy;
}
};
const staticMethodCache = {};
Expand Down Expand Up @@ -227,6 +265,10 @@ function initUTSClassName(moduleName, className, is_uni_modules) {
}
return '';
}
const interfaceDefines = {};
function registerUTSInterface(name, define) {
interfaceDefines[name] = define;
}
const pluginDefines = {};
function registerUTSPlugin(name, define) {
pluginDefines[name] = define;
Expand All @@ -239,4 +281,4 @@ function requireUTSPlugin(name) {
return define;
}

export { initUTSClassName, initUTSIndexClassName, initUTSPackageName, initUTSProxyClass, initUTSProxyFunction, normalizeArg, registerUTSPlugin, requireUTSPlugin };
export { initUTSClassName, initUTSIndexClassName, initUTSPackageName, initUTSProxyClass, initUTSProxyFunction, normalizeArg, registerUTSInterface, registerUTSPlugin, requireUTSPlugin };

0 comments on commit 1a9113a

Please sign in to comment.