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

wip(uts): compiler

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