提交 6eae1e9f 编写于 作者: fxy060608's avatar fxy060608

wip(uts): compiler

上级 7efd1e5c
......@@ -86,7 +86,7 @@ describe('uts-module', () => {
const wifi = new WifiManager()
wifi.preparePermission(1, 2, 3, () => {})
wifi.count
wifi.staticCount
wifi.staticPreparePermission(1)
WifiManager.staticCount
WifiManager.staticPreparePermission(1)
})
})
......@@ -169,7 +169,7 @@ function resolveSyncResult(res) {
function invokePropGetter(args) {
return resolveSyncResult(getProxy().invokeSync(args, () => { }));
}
function initProxyFunction(async, { package: pkg, class: cls, name: propOrMethod, id: instanceId, }) {
function initProxyFunction(async, { package: pkg, class: cls, name: propOrMethod, id: instanceId, companion, }) {
const invokeCallback = ({ id, name, params, keepAlive, }) => {
const callback = callbacks[id];
if (callback) {
......@@ -188,6 +188,7 @@ function initProxyFunction(async, { package: pkg, class: cls, name: propOrMethod
package: pkg,
class: cls,
name: propOrMethod,
companion,
};
return (...args) => {
const invokeArgs = shared.extend({}, baseArgs, {
......@@ -222,7 +223,7 @@ function initUtsProxyClass({ package: pkg, class: cls, methods, props, staticPro
package: pkg,
class: cls,
};
return class ProxyClass {
const ProxyClass = class UtsClass {
constructor(...params) {
const target = {};
// 初始化实例 ID
......@@ -237,24 +238,33 @@ function initUtsProxyClass({ package: pkg, class: cls, methods, props, staticPro
name,
}, baseOptions));
}
else if (shared.hasOwn(staticMethods, name)) {
// 静态方法
target[name] = initUtsStaticMethod(!!staticMethods[name].async, shared.extend({ name, companion: true }, baseOptions));
}
else if (props.includes(name)) {
// 实例属性
return invokePropGetter({ id: instanceId, name: name });
}
else if (staticProps.includes(name)) {
// 静态属性
return invokePropGetter(shared.extend({ name: name, companion: true }, baseOptions));
}
}
return target[name];
},
});
}
};
const staticMethodCache = {};
return new Proxy(ProxyClass, {
get(target, name, receiver) {
if (shared.hasOwn(staticMethods, name)) {
if (!staticMethodCache[name]) {
// 静态方法
staticMethodCache[name] = initUtsStaticMethod(!!staticMethods[name].async, shared.extend({ name, companion: true }, baseOptions));
}
return staticMethodCache[name];
}
if (staticProps.includes(name)) {
// 静态属性
return invokePropGetter(shared.extend({ name: name, companion: true }, baseOptions));
}
return Reflect.get(target, name, receiver);
},
});
}
exports.getCurrentSubNVue = getCurrentSubNVue;
......
......@@ -137,7 +137,7 @@ function resolveSyncResult(res) {
function invokePropGetter(args) {
return resolveSyncResult(getProxy().invokeSync(args, () => { }));
}
function initProxyFunction(async, { package: pkg, class: cls, name: propOrMethod, id: instanceId, }) {
function initProxyFunction(async, { package: pkg, class: cls, name: propOrMethod, id: instanceId, companion, }) {
const invokeCallback = ({ id, name, params, keepAlive, }) => {
const callback = callbacks[id];
if (callback) {
......@@ -156,6 +156,7 @@ function initProxyFunction(async, { package: pkg, class: cls, name: propOrMethod
package: pkg,
class: cls,
name: propOrMethod,
companion,
};
return (...args) => {
const invokeArgs = extend({}, baseArgs, {
......@@ -190,7 +191,7 @@ function initUtsProxyClass({ package: pkg, class: cls, methods, props, staticPro
package: pkg,
class: cls,
};
return class ProxyClass {
const ProxyClass = class UtsClass {
constructor(...params) {
const target = {};
// 初始化实例 ID
......@@ -205,24 +206,33 @@ function initUtsProxyClass({ package: pkg, class: cls, methods, props, staticPro
name,
}, baseOptions));
}
else if (hasOwn(staticMethods, name)) {
// 静态方法
target[name] = initUtsStaticMethod(!!staticMethods[name].async, extend({ name, companion: true }, baseOptions));
}
else if (props.includes(name)) {
// 实例属性
return invokePropGetter({ id: instanceId, name: name });
}
else if (staticProps.includes(name)) {
// 静态属性
return invokePropGetter(extend({ name: name, companion: true }, baseOptions));
}
}
return target[name];
},
});
}
};
const staticMethodCache = {};
return new Proxy(ProxyClass, {
get(target, name, receiver) {
if (hasOwn(staticMethods, name)) {
if (!staticMethodCache[name]) {
// 静态方法
staticMethodCache[name] = initUtsStaticMethod(!!staticMethods[name].async, extend({ name, companion: true }, baseOptions));
}
return staticMethodCache[name];
}
if (staticProps.includes(name)) {
// 静态属性
return invokePropGetter(extend({ name: name, companion: true }, baseOptions));
}
return Reflect.get(target, name, receiver);
},
});
}
export { getCurrentSubNVue, getSsrGlobalData, initUtsProxyClass, initUtsProxyFunction, onAddToFavorites, onBackPress, onError, onHide, onInit, onLaunch, onLoad, onNavigationBarButtonTap, onNavigationBarSearchInputChanged, onNavigationBarSearchInputClicked, onNavigationBarSearchInputConfirmed, onNavigationBarSearchInputFocusChanged, onPageNotFound, onPageScroll, onPullDownRefresh, onReachBottom, onReady, onResize, onSaveExitState, onShareAppMessage, onShareTimeline, onShow, onTabItemTap, onThemeChange, onUnhandledRejection, onUnload, requireNativePlugin, resolveEasycom, shallowSsrRef, ssrRef };
......@@ -133,6 +133,10 @@ interface InitProxyFunctionOptions {
* 属性名或方法名
*/
name: string
/**
* 是否伴生对象
*/
companion?: boolean
/**
* 实例 ID
*/
......@@ -146,6 +150,7 @@ function initProxyFunction(
class: cls,
name: propOrMethod,
id: instanceId,
companion,
}: InitProxyFunctionOptions
) {
const invokeCallback = ({
......@@ -170,6 +175,7 @@ function initProxyFunction(
package: pkg,
class: cls,
name: propOrMethod,
companion,
}
return (...args: unknown[]) => {
const invokeArgs = extend({}, baseArgs, {
......@@ -198,6 +204,7 @@ function initUtsStaticMethod(async: boolean, opts: ProxyBaseOptions) {
return initProxyFunction(async, opts)
}
export const initUtsProxyFunction = initUtsStaticMethod
export function initUtsProxyClass({
package: pkg,
class: cls,
......@@ -210,7 +217,7 @@ export function initUtsProxyClass({
package: pkg,
class: cls,
}
return class ProxyClass {
const ProxyClass = class UtsClass {
constructor(...params: unknown[]) {
const target: Record<string, Function> = {}
// 初始化实例 ID
......@@ -234,20 +241,9 @@ export function initUtsProxyClass({
baseOptions
)
)
} else if (hasOwn(staticMethods, name)) {
// 静态方法
target[name] = initUtsStaticMethod(
!!staticMethods[name].async,
extend({ name, companion: true }, baseOptions)
)
} else if (props.includes(name as string)) {
// 实例属性
return invokePropGetter({ id: instanceId, name: name as string })
} else if (staticProps.includes(name as string)) {
// 静态属性
return invokePropGetter(
extend({ name: name as string, companion: true }, baseOptions)
)
}
}
return target[name as string]
......@@ -255,4 +251,26 @@ export function initUtsProxyClass({
})
}
}
const staticMethodCache: Record<string, Function> = {}
return new Proxy(ProxyClass, {
get(target, name, receiver) {
if (hasOwn(staticMethods, name)) {
if (!staticMethodCache[name as string]) {
// 静态方法
staticMethodCache[name] = initUtsStaticMethod(
!!staticMethods[name].async,
extend({ name, companion: true }, baseOptions)
)
}
return staticMethodCache[name]
}
if (staticProps.includes(name as string)) {
// 静态属性
return invokePropGetter(
extend({ name: name as string, companion: true }, baseOptions)
)
}
return Reflect.get(target, name, receiver)
},
})
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册