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

wip(uts): compiler

上级 9c2e7276
......@@ -9,8 +9,29 @@ class GetBatteryInfoOptions : UTSJSONObject {
public var fail: UTSCallback?;
public var complete: UTSCallback?;
}
func getBatteryInfo(_ options: GetBatteryInfoOptions) {
var res = [
"errMsg": "getBatteryInfo:ok",
"level": UIDevice.current.batteryLevel * 100,
"isCharging": UIDevice.current.batteryState == UIDevice.BatteryState.charging
] as [String: Any];
if (options.success != nil) {
options.success!(res);
}
if (options.complete != nil) {
options.complete!(res);
}
}
func test1(_ callback: UTSCallback) {
console.log("test1", " at uni_modules/test-uniplugin/app-ios/index.uts:26");
console.log("def ios", " at uni_modules/test-uniplugin/app-ios/index.uts:31");
console.log("ndef android", " at uni_modules/test-uniplugin/app-ios/index.uts:34");
console.log("def android || def ios", " at uni_modules/test-uniplugin/app-ios/index.uts:40");
}
class Test1 : NSObject {
}
@objc(UTSSDKModulesTestUniPluginTest)
@objcMembers
class Test : NSObject {
public init(){
Test1();
......@@ -22,23 +43,10 @@ class Test : NSObject {
@objc(UTSSDKModulesTestUniPluginIndexSwift)
@objcMembers
class IndexSwift : NSObject {
public static func getBatteryInfo(_ options: GetBatteryInfoOptions) {
var res = [
"errMsg": "getBatteryInfo:ok",
"level": UIDevice.current.batteryLevel * 100,
"isCharging": UIDevice.current.batteryState == UIDevice.BatteryState.charging
] as [String: Any];
if (options.success != nil) {
options.success!(res);
}
if (options.complete != nil) {
options.complete!(res);
}
public static func s_getBatteryInfo(_ options: GetBatteryInfoOptions) {
return getBatteryInfo(options);
}
public static func test1(_ callback: UTSCallback) {
console.log("test1", " at uni_modules/test-uniplugin/app-ios/index.uts:26");
console.log("def ios", " at uni_modules/test-uniplugin/app-ios/index.uts:31");
console.log("ndef android", " at uni_modules/test-uniplugin/app-ios/index.uts:34");
console.log("def android || def ios", " at uni_modules/test-uniplugin/app-ios/index.uts:40");
public static func s_test1(_ callback: UTSCallback) {
return test1(callback);
}
}
{"version":3,"sources":["/Users/fxy/Projects/GitHub/uni-app/uni-app-next/packages/playground/uts/uni_modules/test-uniplugin/app-ios/index.uts"],"sourcesContent":["import { UIDevice } from 'UIKit'\n\ntype GetBatteryInfoOptions = {\n name: string\n pwd: number\n success?: (res: UTSJSONObject) => void\n fail?: (res: UTSJSONObject) => void\n complete?: (res: UTSJSONObject) => void\n}\n\nexport default function getBatteryInfo(options: GetBatteryInfoOptions) {\n const res = {\n errMsg: 'getBatteryInfo:ok',\n level: UIDevice.current.batteryLevel * 100,\n isCharging: UIDevice.current.batteryState == UIDevice.BatteryState.charging,\n }\n if (options.success != null) {\n options.success!(res)\n }\n if (options.complete != null) {\n options.complete!(res)\n }\n}\n\nexport function test1(callback: () => void) {\n console.log('test1')\n\n\n\n\n console.log('def ios')\n\n\n console.log('ndef android')\n\n\n\n\n\n console.log('def android || def ios')\n\n\n\n\n}\n\nclass Test1 {}\nexport class Test {\n constructor() {\n new Test1()\n }\n test(): string | null {\n return null\n }\n}\n"],"names":[],"mappings":";AAAA;AAEA;;AAA6B,MAAxB,qBAAqB;IACxB,WAAA,IAAI,EAAE,MAAM,EAAA;IACZ,WAAA,GAAG,EAAE,QAAM,EAAA;IACX,WAAA,OAAO,EAAC,WAA8B,EAAA;IACtC,WAAA,IAAI,EAAC,WAA8B,EAAA;IACnC,WAAA,QAAQ,EAAC,WAA8B,EAAA;CACxC;AAsCD,MAAM,KAAK;CAAG;AACP,MAAM,IAAI;IACf,aAAc;QACZ,AAAI,KAAK,EAAE;IACb;gBACA,IAAI,MAAI,MAAM,EAAQ;QACpB,OAAO,GAAI,CAAA;IACb;CACD;;;;uBA5CuB,cAAc,CAAC,EAAA,OAA8B,EAArB,qBAAqB,EAAE;QACrE,IAAM,GAAG,GAAG;YACV,CAAA,MAAM,GAAE,mBAAmB;YAC3B,CAAA,KAAK,GAAE,QAAQ,CAAC,OAAO,CAAC,YAAY,GAAG,GAAG;YAC1C,CAAA,UAAU,GAAE,QAAQ,CAAC,OAAO,CAAC,YAAY,IAAI,QAAQ,CAAC,YAAY,CAAC,QAAQ;0BAC5E;QACD,IAAI,OAAO,CAAC,OAAO,IAAI,GAAI;YACzB,OAAO,CAAC,OAAO,EAAE,GAAG,CAAC;;QAEvB,IAAI,OAAO,CAAC,QAAQ,IAAI,GAAI;YAC1B,OAAO,CAAC,QAAQ,EAAE,GAAG,CAAC;;IAE1B;uBAEgB,KAAK,CAAC,EAAA,QAAoB,aAAA,EAAE;QAC1C,OAAO,CAAC,GAAG,CAAC,OAAO,wDAAC;QAKpB,OAAO,CAAC,GAAG,CAAC,SAAS,wDAAC;QAGtB,OAAO,CAAC,GAAG,CAAC,cAAc,wDAAC;QAM3B,OAAO,CAAC,GAAG,CAAC,wBAAwB,wDAAC;IAKvC"}
\ No newline at end of file
{"version":3,"sources":["/Users/fxy/Projects/GitHub/uni-app/uni-app-next/packages/playground/uts/uni_modules/test-uniplugin/app-ios/index.uts"],"sourcesContent":["import { UIDevice } from 'UIKit'\n\ntype GetBatteryInfoOptions = {\n name: string\n pwd: number\n success?: (res: UTSJSONObject) => void\n fail?: (res: UTSJSONObject) => void\n complete?: (res: UTSJSONObject) => void\n}\n\nexport default function getBatteryInfo(options: GetBatteryInfoOptions) {\n const res = {\n errMsg: 'getBatteryInfo:ok',\n level: UIDevice.current.batteryLevel * 100,\n isCharging: UIDevice.current.batteryState == UIDevice.BatteryState.charging,\n }\n if (options.success != null) {\n options.success!(res)\n }\n if (options.complete != null) {\n options.complete!(res)\n }\n}\n\nexport function test1(callback: () => void) {\n console.log('test1')\n\n\n\n\n console.log('def ios')\n\n\n console.log('ndef android')\n\n\n\n\n\n console.log('def android || def ios')\n\n\n\n\n}\n\nclass Test1 {}\nexport class Test {\n constructor() {\n new Test1()\n }\n test(): string | null {\n return null\n }\n}\n"],"names":[],"mappings":";AAAA;AAE6B;;MAAxB,qBAAqB;IACxB,WAAA,IAAI,EAAE,MAAM,EAAA;IACZ,WAAA,GAAG,EAAE,QAAM,EAAA;IACX,WAAA,OAAO,EAAC,WAA8B,EAAA;IACtC,WAAA,IAAI,EAAC,WAA8B,EAAA;IACnC,WAAA,QAAQ,EAAC,WAA8B,EAAA;CACxC;AAEc,KAAS,cAAc,CAAC,EAAA,OAA8B,EAArB,qBAAqB,EAAE;IACrE,IAAM,GAAG,GAAG;QACV,CAAA,MAAM,GAAE,mBAAmB;QAC3B,CAAA,KAAK,GAAE,QAAQ,CAAC,OAAO,CAAC,YAAY,GAAG,GAAG;QAC1C,CAAA,UAAU,GAAE,QAAQ,CAAC,OAAO,CAAC,YAAY,IAAI,QAAQ,CAAC,YAAY,CAAC,QAAQ;sBAC5E;IACD,IAAI,OAAO,CAAC,OAAO,IAAI,GAAI;QACzB,OAAO,CAAC,OAAO,EAAE,GAAG,CAAC;;IAEvB,IAAI,OAAO,CAAC,QAAQ,IAAI,GAAI;QAC1B,OAAO,CAAC,QAAQ,EAAE,GAAG,CAAC;;AAE1B;AAEO,KAAS,KAAK,CAAC,EAAA,QAAoB,aAAA,EAAE;IAC1C,OAAO,CAAC,GAAG,CAAC,OAAO,wDAAC;IAKpB,OAAO,CAAC,GAAG,CAAC,SAAS,wDAAC;IAGtB,OAAO,CAAC,GAAG,CAAC,cAAc,wDAAC;IAM3B,OAAO,CAAC,GAAG,CAAC,wBAAwB,wDAAC;AAKvC;AAEA,MAAM,KAAK;CAAG;AACP;;MAAM,IAAI;IACf,aAAc;QACZ,AAAI,KAAK,EAAE;IACb;gBACA,IAAI,MAAI,MAAM,EAAQ;QACpB,OAAO,GAAI,CAAA;IACb;CACD;;;;wCA5CsC,EAAA,OAA8B,EAArB,qBAAqB;eAA7C,cAAc,CAAC,OAA8B;;+BAc/C,EAAA,QAAoB,aAAA;eAA1B,KAAK,CAAC,QAAoB"}
\ No newline at end of file
......@@ -183,7 +183,7 @@ function resolveSyncResult(res) {
function invokePropGetter(args) {
return resolveSyncResult(getProxy().invokeSync(args, () => { }));
}
function initProxyFunction(async, { package: pkg, class: cls, name: propOrMethod, companion, params: methodParams, }, instanceId) {
function initProxyFunction(async, { package: pkg, class: cls, name: propOrMethod, method, companion, params: methodParams, }, instanceId) {
const invokeCallback = ({ id, name, params, keepAlive, }) => {
const callback = callbacks[id];
if (callback) {
......@@ -201,7 +201,7 @@ function initProxyFunction(async, { package: pkg, class: cls, name: propOrMethod
: {
package: pkg,
class: cls,
name: propOrMethod,
name: method || propOrMethod,
companion,
method: methodParams,
};
......@@ -230,6 +230,11 @@ function initProxyFunction(async, { package: pkg, class: cls, name: propOrMethod
};
}
function initUtsStaticMethod(async, opts) {
if (opts.main && !opts.method) {
if (typeof plus !== 'undefined' && plus.os.name === 'iOS') {
opts.method = 's_' + opts.name;
}
}
return initProxyFunction(async, opts);
}
const initUtsProxyFunction = initUtsStaticMethod;
......@@ -290,15 +295,24 @@ function initUtsPackageName(name, is_uni_modules) {
}
return '';
}
function initUtsClassName(name, is_uni_modules) {
function initUtsIndexClassName(moduleName, is_uni_modules) {
if (typeof plus === 'undefined') {
return '';
}
return initUtsClassName(moduleName, plus.os.name === 'iOS' ? 'IndexSwift' : 'IndexKt', is_uni_modules);
}
function initUtsClassName(moduleName, className, is_uni_modules) {
if (typeof plus === 'undefined') {
return '';
}
if (plus.os.name === 'Android') {
return 'IndexKt';
return className;
}
if (plus.os.name === 'iOS') {
return 'UTSSDK' + (is_uni_modules ? 'Modules' : '') + shared.capitalize(name);
return ('UTSSDK' +
(is_uni_modules ? 'Modules' : '') +
shared.capitalize(moduleName) +
shared.capitalize(className));
}
return '';
}
......@@ -308,6 +322,7 @@ exports.formatH5Log = formatH5Log;
exports.getCurrentSubNVue = getCurrentSubNVue;
exports.getSsrGlobalData = getSsrGlobalData;
exports.initUtsClassName = initUtsClassName;
exports.initUtsIndexClassName = initUtsIndexClassName;
exports.initUtsPackageName = initUtsPackageName;
exports.initUtsProxyClass = initUtsProxyClass;
exports.initUtsProxyFunction = initUtsProxyFunction;
......
......@@ -39,7 +39,9 @@ export declare function getCurrentSubNVue(): any;
export declare function getSsrGlobalData(): any;
export declare function initUtsClassName(name: string, is_uni_modules: boolean): string;
export declare function initUtsClassName(moduleName: string, className: string, is_uni_modules: boolean): string;
export declare function initUtsIndexClassName(moduleName: string, is_uni_modules: boolean): string;
export declare function initUtsPackageName(name: string, is_uni_modules: boolean): string;
......@@ -203,6 +205,10 @@ declare interface ProxyClassOptions {
}
declare interface ProxyFunctionOptions {
/**
* 是否是入口类
*/
main?: boolean;
/**
* 包名
*/
......@@ -215,6 +221,10 @@ declare interface ProxyFunctionOptions {
* 属性名或方法名
*/
name: string;
/**
* 方法名 指定的方法名(用于 IndexSwift 静态方法,自动补充前缀 s_)
*/
method?: string;
/**
* 是否伴生对象
*/
......
......@@ -151,7 +151,7 @@ function resolveSyncResult(res) {
function invokePropGetter(args) {
return resolveSyncResult(getProxy().invokeSync(args, () => { }));
}
function initProxyFunction(async, { package: pkg, class: cls, name: propOrMethod, companion, params: methodParams, }, instanceId) {
function initProxyFunction(async, { package: pkg, class: cls, name: propOrMethod, method, companion, params: methodParams, }, instanceId) {
const invokeCallback = ({ id, name, params, keepAlive, }) => {
const callback = callbacks[id];
if (callback) {
......@@ -169,7 +169,7 @@ function initProxyFunction(async, { package: pkg, class: cls, name: propOrMethod
: {
package: pkg,
class: cls,
name: propOrMethod,
name: method || propOrMethod,
companion,
method: methodParams,
};
......@@ -198,6 +198,11 @@ function initProxyFunction(async, { package: pkg, class: cls, name: propOrMethod
};
}
function initUtsStaticMethod(async, opts) {
if (opts.main && !opts.method) {
if (typeof plus !== 'undefined' && plus.os.name === 'iOS') {
opts.method = 's_' + opts.name;
}
}
return initProxyFunction(async, opts);
}
const initUtsProxyFunction = initUtsStaticMethod;
......@@ -258,17 +263,26 @@ function initUtsPackageName(name, is_uni_modules) {
}
return '';
}
function initUtsClassName(name, is_uni_modules) {
function initUtsIndexClassName(moduleName, is_uni_modules) {
if (typeof plus === 'undefined') {
return '';
}
return initUtsClassName(moduleName, plus.os.name === 'iOS' ? 'IndexSwift' : 'IndexKt', is_uni_modules);
}
function initUtsClassName(moduleName, className, is_uni_modules) {
if (typeof plus === 'undefined') {
return '';
}
if (plus.os.name === 'Android') {
return 'IndexKt';
return className;
}
if (plus.os.name === 'iOS') {
return 'UTSSDK' + (is_uni_modules ? 'Modules' : '') + capitalize(name);
return ('UTSSDK' +
(is_uni_modules ? 'Modules' : '') +
capitalize(moduleName) +
capitalize(className));
}
return '';
}
export { formatAppLog, formatH5Log, getCurrentSubNVue, getSsrGlobalData, initUtsClassName, initUtsPackageName, 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 { formatAppLog, formatH5Log, getCurrentSubNVue, getSsrGlobalData, initUtsClassName, initUtsIndexClassName, initUtsPackageName, 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 };
......@@ -6,6 +6,7 @@ export * from './apiLifecycle'
export {
initUtsProxyClass,
initUtsProxyFunction,
initUtsIndexClassName,
initUtsClassName,
initUtsPackageName,
} from './uts'
......@@ -26,6 +26,10 @@ interface Parameter {
type: string
}
interface ProxyFunctionOptions {
/**
* 是否是入口类
*/
main?: boolean
/**
* 包名
*/
......@@ -38,6 +42,10 @@ interface ProxyFunctionOptions {
* 属性名或方法名
*/
name: string
/**
* 方法名 指定的方法名(用于 IndexSwift 静态方法,自动补充前缀 s_)
*/
method?: string
/**
* 是否伴生对象
*/
......@@ -153,6 +161,7 @@ function initProxyFunction(
package: pkg,
class: cls,
name: propOrMethod,
method,
companion,
params: methodParams,
}: ProxyFunctionOptions,
......@@ -179,7 +188,7 @@ function initProxyFunction(
: {
package: pkg,
class: cls,
name: propOrMethod,
name: method || propOrMethod,
companion,
method: methodParams,
}
......@@ -207,8 +216,14 @@ function initProxyFunction(
}
function initUtsStaticMethod(async: boolean, opts: ProxyFunctionOptions) {
if (opts.main && !opts.method) {
if (typeof plus !== 'undefined' && plus.os.name === 'iOS') {
opts.method = 's_' + opts.name
}
}
return initProxyFunction(async, opts)
}
export const initUtsProxyFunction = initUtsStaticMethod
export function initUtsProxyClass({
......@@ -292,15 +307,38 @@ export function initUtsPackageName(name: string, is_uni_modules: boolean) {
return ''
}
export function initUtsClassName(name: string, is_uni_modules: boolean) {
export function initUtsIndexClassName(
moduleName: string,
is_uni_modules: boolean
) {
if (typeof plus === 'undefined') {
return ''
}
return initUtsClassName(
moduleName,
plus.os.name === 'iOS' ? 'IndexSwift' : 'IndexKt',
is_uni_modules
)
}
export function initUtsClassName(
moduleName: string,
className: string,
is_uni_modules: boolean
) {
if (typeof plus === 'undefined') {
return ''
}
if (plus.os.name === 'Android') {
return 'IndexKt'
return className
}
if (plus.os.name === 'iOS') {
return 'UTSSDK' + (is_uni_modules ? 'Modules' : '') + capitalize(name)
return (
'UTSSDK' +
(is_uni_modules ? 'Modules' : '') +
capitalize(moduleName) +
capitalize(className)
)
}
return ''
}
......@@ -29,11 +29,12 @@ export async function genProxyCode(
) {
const { name, is_uni_modules } = options
return `
import { initUtsProxyClass, initUtsProxyFunction, initUtsPackageName, initUtsClassName } from '@dcloudio/uni-app'
import { extend } from '@vue/shared'
import { initUtsProxyClass, initUtsProxyFunction, initUtsPackageName, initUtsIndexClassName, initUtsClassName } from '@dcloudio/uni-app'
const name = '${name}'
const is_uni_modules = ${is_uni_modules}
const pkg = initUtsPackageName(name, is_uni_modules)
const cls = initUtsClassName(name, is_uni_modules)
const cls = initUtsIndexClassName(name, is_uni_modules)
${genModuleCode(await parseModuleDecls(module, options))}
`
}
......@@ -63,17 +64,17 @@ function genModuleCode(decls: ProxyDecl[]) {
if (decl.type === 'Class') {
if (decl.isDefault) {
codes.push(
`export default initUtsProxyClass({ package: pkg, class: '${
`export default initUtsProxyClass(extend({ package: pkg, class: initUtsClassName(name, '${
decl.cls
}', ...${JSON.stringify(decl.options)} })`
}', is_uni_modules) }, ${JSON.stringify(decl.options)} ))`
)
} else {
codes.push(
`export const ${
decl.cls
} = initUtsProxyClass({ package: pkg, class: '${
} = initUtsProxyClass(extend({ package: pkg, class: initUtsClassName(name, '${
decl.cls
}', ...${JSON.stringify(decl.options)} })`
}', is_uni_modules) }, ${JSON.stringify(decl.options)} ))`
)
}
} else if (decl.type === 'FunctionDeclaration') {
......@@ -81,7 +82,7 @@ function genModuleCode(decls: ProxyDecl[]) {
codes.push(
`export default initUtsProxyFunction(${
decl.async
}, { package: pkg, class: cls, name: '${
}, { main: true, package: pkg, class: cls, name: '${
decl.method
}', params: ${JSON.stringify(decl.params)}})`
)
......@@ -89,7 +90,7 @@ function genModuleCode(decls: ProxyDecl[]) {
codes.push(
`export const ${decl.method} = initUtsProxyFunction(${
decl.async
}, { package: pkg, class: cls, name: '${
}, { main: true, package: pkg, class: cls, name: '${
decl.method
}', params: ${JSON.stringify(decl.params)}})`
)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册