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

wip(uts): compiler

上级 9c87da67
......@@ -19,7 +19,11 @@ module.exports = {
watchPathIgnorePatterns: ['/node_modules/', '/dist/', '/.git/'],
moduleFileExtensions: ['ts', 'tsx', 'js', 'json'],
moduleNameMapper: {
'^@dcloudio/(.*?)$': '<rootDir>/packages/$1/src',
"@dcloudio/uts-darwin-arm64": "<rootDir>/packages/uts-darwin-arm64",
"@dcloudio/uts-darwin-x64": "<rootDir>/packages/uts-darwin-x64",
"@dcloudio/uts-win32-ia32-msvc": "<rootDir>/packages/uts-win32-ia32-msvc",
"@dcloudio/uts-win32-x64-msvc": "<rootDir>/packages/uts-win32-x64-msvc",
'^@dcloudio/(.*?)$': '<rootDir>/packages/$1/src'
},
rootDir: __dirname,
testMatch: ['<rootDir>/packages/**/__tests__/**/*spec.[jt]s?(x)'],
......
......@@ -42,13 +42,16 @@ export class User implements IUser {
.setContentView(R.layout.toast_hint)
.setDuration(1000)
.setImageDrawable(android.R.id.icon, R.mipmap.ic_dialog_tip_finish)
.setText(android.R.id.message, "点我消失")
.setText(android.R.id.message, '点我消失')
.show()
}
register(name: string, callback: () => void) {
Log.info(logo as FrameLayout)
}
test(view: View) { }
test(view: View) {}
}
function login(name: string, callback: () => void) { }
export function register(name: string, callback: () => void) { }
function login(name: string, callback: () => void) {}
export function register(name: string, callback: () => void) {}
export function offMemoryWarning(
callback: null | ((level: number) => void) = null
) {}
{"version":3,"sources":["/Users/fxy/Projects/GitHub/uni-app/uni-app-next/packages/playground/uts/uni_modules/test-uniplugin/static/logo.png","/Users/fxy/Projects/GitHub/uni-app/uni-app-next/packages/playground/uts/uni_modules/test-uniplugin/utssdk/app-android/index.uts","/Users/fxy/Projects/GitHub/uni-app/uni-app-next/packages/playground/uts/uni_modules/test-uniplugin/utssdk/app-android/interface.uts","/Users/fxy/Projects/GitHub/uni-app/uni-app-next/packages/playground/uts/uni_modules/test-uniplugin/utssdk/app-android/utils.uts","/Users/fxy/Projects/GitHub/uni-app/uni-app-next/packages/playground/uts/uni_modules/test-uniplugin/utssdk/app-android/login.uts"],"sourcesContent":["import { getResourcePath } from 'io.dcloud.uts.android'\nexport default getResourcePath('uni_modules/test-uniplugin/static/logo.png')\n ","import Log from 'android.util.Log'\nimport FrameLayout from 'android.widget.FrameLayout'\nimport View from 'android.view.View'\nimport { IUser } from './interface.uts'\nimport { login } from './login.uts'\nimport logo from '../../static/logo.png'\n\nconst test = arrayOf(1, 2, 3)\n\ntype GetBatteryInfoOptions = {\n success?: (res: UTSJSONObject) => void\n fail?: (res: UTSJSONObject) => void\n complete?: (res: UTSJSONObject) => void\n}\nexport class User implements IUser {\n async login(name: string, pwd: string) {\n setTimeout(() => {\n console.log('timeout')\n }, 1000)\n login(name, pwd)\n Log.info(logo)\n\n console.log('def android')\n\n\n\n\n\n\n\n\n console.log('ndef ios')\n\n\n console.log('def android || def ios')\n\n\n\n\n\n new XToast<XToast<unknown>>(getUniActivity())\n .setContentView(R.layout.toast_hint)\n .setDuration(1000)\n .setImageDrawable(android.R.id.icon, R.mipmap.ic_dialog_tip_finish)\n .setText(android.R.id.message, \"点我消失\")\n .show()\n }\n register(name: string, callback: () => void) {\n Log.info(logo as FrameLayout)\n }\n test(view: View) { }\n}\nfunction login(name: string, callback: () => void) { }\nexport function register(name: string, callback: () => void) { }\n","export interface IUser {\n register(name: string): void\n}\n","export function test(){\n console.log('test')\n}\n","import { test } from \"./utils.uts\"\nexport function login(name: string, pwd: string) {\n console.log('login')\n test()\n return { name, pwd }\n}\n"],"names":[],"mappings":";;;;;;AAAA;ACAA,OAAgB,gBAAkB,CAAA;AAClC,OAAwB,0BAA4B,CAAA;AACpD,OAAiB,iBAAmB,CAAA;UCFnB;QACf,SAAS,MAAM,MAAM,GAAG,IAAI;;ACDvB,IAAS,OAAM;IAClB,QAAQ,GAAG,CAAC;AAChB;ACDO,IAAS,MAAM,MAAM,MAAM,EAAE,KAAK,MAAM,iBAAE;IAC/C,QAAQ,GAAG,CAAC;IACZ;IACA,OAAO;QAAE,IAAA,OAAA;QAAM,IAAA,MAAA;KAAK;AACtB;cJJe,gBAAgB;ACM/B,IAAM,QAAO,QAAQ,CAAC,EAAE,CAAC,EAAE,CAAC;AAEC,WAAxB;IACH,SAAA,SAAQ,oBAA8B;IACtC,SAAA,MAAK,oBAA8B;IACnC,SAAA,UAAS,oBAA8B;AACzC;AACO,WAAM,OAAgB;IAC3B,iBAAM,MAAM,MAAM,MAAM,EAAE,KAAK,MAAM,8CAAE;QACrC,WAAW,KAAM;YACf,QAAQ,GAAG,CAAC;QACd;UAAG,IAAI;QACP,MAAM,MAAM;QACZ,IAAI,IAAI;QAER,QAAQ,GAAG,CAAC;QASZ,QAAQ,GAAG,CAAC;QAGZ,QAAQ,GAAG,CAAC;QAMR,OAAO,OAAO,CAAO,GAAG,kBACzB,cAAc,CAAC,EAAE,MAAM,CAAC,UAAU,EAClC,WAAW,CAAC,IAAI,EAChB,gBAAgB,CAAC,QAAQ,CAAC,CAAC,EAAE,CAAC,IAAI,EAAE,EAAE,MAAM,CAAC,oBAAoB,EACjE,OAAO,CAAC,QAAQ,CAAC,CAAC,EAAE,CAAC,OAAO,EAAE,QAC9B,IAAI;IACT;IACA,aAAA,SAAS,MAAM,MAAM,EAAE,qBAAoB,EAAE;QAC3C,IAAI,IAAI,YAAS;IACnB;IACA,SAAA,KAAK,MAAM,IAAI,EAAE,CAAE;AACrB;AACA,UAAe,MAAM,MAAM,EAAE,gBAAgB,IAAI,EAAE,CAAE;AAC9C,IAAS,SAAS,MAAM,MAAM,EAAE,qBAAoB,EAAE,CAAE"}
\ 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/static/logo.png","/Users/fxy/Projects/GitHub/uni-app/uni-app-next/packages/playground/uts/uni_modules/test-uniplugin/utssdk/app-android/index.uts","/Users/fxy/Projects/GitHub/uni-app/uni-app-next/packages/playground/uts/uni_modules/test-uniplugin/utssdk/app-android/interface.uts","/Users/fxy/Projects/GitHub/uni-app/uni-app-next/packages/playground/uts/uni_modules/test-uniplugin/utssdk/app-android/utils.uts","/Users/fxy/Projects/GitHub/uni-app/uni-app-next/packages/playground/uts/uni_modules/test-uniplugin/utssdk/app-android/login.uts"],"sourcesContent":["import { getResourcePath } from 'io.dcloud.uts.android'\nexport default getResourcePath('uni_modules/test-uniplugin/static/logo.png')\n ","import Log from 'android.util.Log'\nimport FrameLayout from 'android.widget.FrameLayout'\nimport View from 'android.view.View'\nimport { IUser } from './interface.uts'\nimport { login } from './login.uts'\nimport logo from '../../static/logo.png'\n\nconst test = arrayOf(1, 2, 3)\n\ntype GetBatteryInfoOptions = {\n success?: (res: UTSJSONObject) => void\n fail?: (res: UTSJSONObject) => void\n complete?: (res: UTSJSONObject) => void\n}\nexport class User implements IUser {\n async login(name: string, pwd: string) {\n setTimeout(() => {\n console.log('timeout')\n }, 1000)\n login(name, pwd)\n Log.info(logo)\n\n console.log('def android')\n\n\n\n\n\n\n\n\n console.log('ndef ios')\n\n\n console.log('def android || def ios')\n\n\n\n\n\n new XToast<XToast<unknown>>(getUniActivity())\n .setContentView(R.layout.toast_hint)\n .setDuration(1000)\n .setImageDrawable(android.R.id.icon, R.mipmap.ic_dialog_tip_finish)\n .setText(android.R.id.message, '点我消失')\n .show()\n }\n register(name: string, callback: () => void) {\n Log.info(logo as FrameLayout)\n }\n test(view: View) {}\n}\nfunction login(name: string, callback: () => void) {}\nexport function register(name: string, callback: () => void) {}\nexport function offMemoryWarning(\n callback: null | ((level: number) => void) = null\n) {}\n","export interface IUser {\n register(name: string): void\n}\n","export function test(){\n console.log('test')\n}\n","import { test } from \"./utils.uts\"\nexport function login(name: string, pwd: string) {\n console.log('login')\n test()\n return { name, pwd }\n}\n"],"names":[],"mappings":";;;;;;AAAA;ACAA,OAAgB,gBAAkB,CAAA;AAClC,OAAwB,0BAA4B,CAAA;AACpD,OAAiB,iBAAmB,CAAA;UCFnB;QACf,SA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: IUser {
}
fun login(name: String, callback: () -> Unit) {}
fun register(name: String, callback: UTSCallback) {}
fun offMemoryWarning(callback: (UTSCallback)? = null) {}
......@@ -2246,6 +2246,9 @@ const METHODS = {
moveAlong(ctx, args) {
return invokeVmMethod(ctx, 'moveAlong', args);
},
setLocMarkerIcon(ctx, args) {
return invokeVmMethod(ctx, 'setLocMarkerIcon', args);
},
openMapApp(ctx, args) {
return invokeVmMethod(ctx, 'openMapApp', args);
},
......@@ -9566,6 +9569,9 @@ class MapContext {
moveAlong(options) {
operateMapWrap(this.id, this.pageId, 'moveAlong', options);
}
setLocMarkerIcon(options) {
operateMapWrap(this.id, this.pageId, 'setLocMarkerIcon', options);
}
openMapApp(options) {
operateMapWrap(this.id, this.pageId, 'openMapApp', options);
}
......
......@@ -51,25 +51,25 @@ describe('uts-module', () => {
/**
* {"package":"testPlugin","class":"","method":"preparePermission","params":[{"name":"foo","age":10,"success":7,"fail":8},9]}
*/
preparePermission(
{
family: {
father: 'f',
mother: 'm',
},
name: 'foo',
age: 10,
success(res: any) {
console.log('success', res)
},
fail(res: any) {
console.log('fail', res)
},
const options = {
family: {
father: 'f',
mother: 'm',
},
name: 'foo',
age: 10,
success(res: any) {
console.log('success', res)
},
fail(res: any) {
console.log('fail', res)
},
(res: any) => {
console.log('callback', res)
}
)
}
const callback = (res: any) => {
console.log('callback', res)
}
preparePermission(options, callback)
preparePermission(options, callback)
})
})
test(`initProxyClass`, () => {
......
......@@ -154,7 +154,9 @@ let proxy;
const callbacks = {};
function normalizeArg(arg) {
if (typeof arg === 'function') {
const id = callbackId++;
// 查找该函数是否已缓存
const oldId = Object.keys(callbacks).find((id) => callbacks[id] === arg);
const id = oldId ? parseInt(oldId) : callbackId++;
callbacks[id] = arg;
return id;
}
......
......@@ -123,7 +123,9 @@ let proxy;
const callbacks = {};
function normalizeArg(arg) {
if (typeof arg === 'function') {
const id = callbackId++;
// 查找该函数是否已缓存
const oldId = Object.keys(callbacks).find((id) => callbacks[id] === arg);
const id = oldId ? parseInt(oldId) : callbackId++;
callbacks[id] = arg;
return id;
}
......
......@@ -4,9 +4,12 @@ declare const plus: any
let callbackId = 1
let proxy: any
const callbacks: Record<string, Function> = {}
export function normalizeArg(arg: unknown) {
if (typeof arg === 'function') {
const id = callbackId++
// 查找该函数是否已缓存
const oldId = Object.keys(callbacks).find((id) => callbacks[id] === arg)
const id = oldId ? parseInt(oldId) : callbackId++
callbacks[id] = arg
return id
} else if (isPlainObject(arg)) {
......
......@@ -26,5 +26,7 @@ export default {
'mp.360.unsupported': '360 is unsupported',
'file.notfound': '{file} is not found',
'uts.ios.tips':
'The project uses the uts plug-in. After the uts plug-in code is modified, the [Custom playground native runner](https://uniapp.dcloud.net.cn/tutorial/run/run-app.html#customplayground) needs to be regenerated to take effect',
'The project uses the uts plugin. After the uts plug-in code is modified, the [Custom playground native runner](https://uniapp.dcloud.net.cn/tutorial/run/run-app.html#customplayground) needs to be regenerated to take effect',
'uts.android.compiler.server':
'The project uses the uts plugin, installing the uts Android runtime extension...',
} as const
......@@ -27,4 +27,6 @@ export default {
'file.notfound': '{file} 文件不存在',
'uts.ios.tips':
'项目使用了uts插件,iOS平台uts插件代码修改后需要重新生成[自定义基座](https://uniapp.dcloud.net.cn/tutorial/run/run-app.html#customplayground)才能生效',
'uts.android.compiler.server':
'项目使用了uts插件,正在安装 uts Android 运行扩展...',
} as const
......@@ -11112,6 +11112,12 @@ const TabBar = /* @__PURE__ */ defineSystemComponent({
tabBar2.color = tabBarStyle.color;
tabBar2.selectedColor = tabBarStyle.selectedColor;
tabBar2.blurEffect = tabBarStyle.blurEffect;
if (tabBarStyle.list.length) {
tabBarStyle.list.forEach((item, index2) => {
tabBar2.list[index2].iconPath = item.iconPath;
tabBar2.list[index2].selectedIconPath = item.selectedIconPath;
});
}
});
return () => {
const tabBarItemsTsx = createTabBarItemsTsx(tabBar2, onSwitchTab, visibleList);
......
......@@ -3532,6 +3532,9 @@ class MapContext {
moveAlong(options) {
operateMapWrap(this.id, this.pageId, "moveAlong", options);
}
setLocMarkerIcon(options) {
operateMapWrap(this.id, this.pageId, "setLocMarkerIcon", options);
}
openMapApp(options) {
operateMapWrap(this.id, this.pageId, "openMapApp", options);
}
......@@ -21518,6 +21521,12 @@ const TabBar = /* @__PURE__ */ defineSystemComponent({
tabBar2.color = tabBarStyle.color;
tabBar2.selectedColor = tabBarStyle.selectedColor;
tabBar2.blurEffect = tabBarStyle.blurEffect;
if (tabBarStyle.list.length) {
tabBarStyle.list.forEach((item, index2) => {
tabBar2.list[index2].iconPath = item.iconPath;
tabBar2.list[index2].selectedIconPath = item.selectedIconPath;
});
}
});
onMounted(() => {
if (tabBar2.iconfontSrc) {
......
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`code genProxyCode 1`] = `
"
import { initUtsProxyClass, initUtsProxyFunction, initUtsPackageName, initUtsIndexClassName, initUtsClassName } from '@dcloudio/uni-app'
const name = 'test-uts'
const is_uni_modules = false
const pkg = initUtsPackageName(name, is_uni_modules)
const cls = initUtsIndexClassName(name, is_uni_modules)
export const onMemoryWarning = initUtsProxyFunction(false, { main: true, package: pkg, class: cls, name: 'onMemoryWarning', params: [{\\"name\\":\\"callback\\",\\"type\\":\\"UTSCallback\\"}]})
export const offMemoryWarning = initUtsProxyFunction(false, { main: true, package: pkg, class: cls, name: 'offMemoryWarning', params: [{\\"name\\":\\"callback\\",\\"type\\":\\"UTSCallback\\",\\"default\\":\\"UTSNull\\"}]})
"
`;
import { resolve } from 'path'
import { genProxyCode } from '../src/code'
const pluginDir = resolve(__dirname, 'examples/uts/utssdk/test-uts')
describe('code', () => {
test('genProxyCode', async () => {
expect(
await genProxyCode(pluginDir, {
is_uni_modules: false,
name: 'test-uts',
namespace: 'uts.sdk.testUts',
})
).toMatchSnapshot()
})
})
export function onMemoryWarning(callback: (level: number) => void) {}
export function offMemoryWarning(
callback: ((level: number) => void) | null = null
) {}
\ No newline at end of file
......@@ -298,10 +298,24 @@ function genProxyClass(
interface Parameter {
name: string
type: string
default?: string | number | boolean
}
type ResolveTypeReferenceName = (name: string) => string
function resolveIdentifierDefaultValue(ident: Expression) {
if (ident.type === 'NullLiteral') {
return 'UTSNull'
} else if (
ident.type === 'StringLiteral' ||
ident.type === 'NumericLiteral' ||
ident.type === 'BooleanLiteral'
) {
return ident.value
}
return null
}
function resolveIdentifierType(
ident: BindingIdentifier,
resolveTypeReferenceName: ResolveTypeReferenceName
......@@ -317,6 +331,26 @@ function resolveIdentifierType(
typeAnnotation.typeName.type === 'Identifier'
) {
return resolveTypeReferenceName(typeAnnotation.typeName.value)
} else if (typeAnnotation.type === 'TsUnionType') {
if (typeAnnotation.types.length === 2) {
const [type1, type2] = typeAnnotation.types
if (type1.type === 'TsKeywordType' && type1.kind === 'null') {
if (
type2.type === 'TsParenthesizedType' &&
type2.typeAnnotation.type === 'TsFunctionType'
) {
return 'UTSCallback'
}
}
if (type2.type === 'TsKeywordType' && type2.kind === 'null') {
if (
type1.type === 'TsParenthesizedType' &&
type1.typeAnnotation.type === 'TsFunctionType'
) {
return 'UTSCallback'
}
}
}
}
}
return ''
......@@ -336,6 +370,21 @@ function resolveFunctionParams(
resolveTypeReferenceName
),
})
} else if (pat.type === 'AssignmentPattern') {
if (pat.left.type === 'Identifier') {
const param: Parameter = {
name: pat.left.value,
type: resolveIdentifierType(
pat.left as BindingIdentifier,
resolveTypeReferenceName
),
}
const defaultValue = resolveIdentifierDefaultValue(pat.right)
if (defaultValue !== null) {
param.default = defaultValue
}
result.push(param)
}
} else {
result.push({ name: '', type: '' })
}
......
......@@ -75,7 +75,7 @@ export async function runKotlinDev(
if (fs.existsSync(kotlinFile)) {
const compilerServer = getCompilerServer()
if (!compilerServer) {
return
throw `项目使用了uts插件,正在安装 uts Android 运行扩展...`
}
const {
getDefaultJar,
......@@ -124,6 +124,10 @@ export async function runKotlinDev(
path.relative(process.env.UNI_OUTPUT_DIR, dexFile)
)
}
} else {
throw `${normalizePath(
path.relative(process.env.UNI_INPUT_DIR, filename)
)} 编译失败`
}
}
return result
......
......@@ -7,6 +7,7 @@ import {
NullablePosition,
SourceMapConsumer,
} from 'source-map'
import { isWindows } from './shared'
const EXTNAME = {
kotlin: '.kt',
......@@ -95,7 +96,7 @@ export function generatedPositionFor({
}: PositionFor): Promise<NullablePosition> {
return resolveSourceMapConsumer(sourceMapFile).then((consumer) => {
return consumer.generatedPositionFor({
source: filename,
source: isWindows ? `\\\\?\\` : '' + filename,
line,
column,
})
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册