Skip to content
This repository was archived by the owner on May 31, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: CI

on:
pull_request:
push:
branches:
- main

jobs:
test:
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- name: Enable Corepack
run: corepack enable
- name: Install dependencies
run: yarn install --immutable
- name: Type check
run: yarn typecheck
- name: Test
run: yarn test
11 changes: 10 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
node_modules
lib
lib

# Yarn (Berry) - 不使用 Zero-Installs
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions
.pnp.*
8 changes: 8 additions & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
approvedGitRepositories:
- "**"

enableScripts: true

nodeLinker: node-modules

npmMinimalAgeGate: 0
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
"types.d.ts"
],
"scripts": {
"test": "echo \"no test specified\"",
"build": "tsc",
"typecheck": "tsc --noEmit",
"test": "yarn test:unit",
"test:unit": "yarn build && node --test ./test/**/*.test.mjs",
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s",
"version": "npm run changelog && git add CHANGELOG.md"
"version": "yarn changelog && git add CHANGELOG.md"
},
"dependencies": {
"change-case-all": "^2.1.0",
Expand All @@ -27,5 +30,6 @@
"devDependencies": {
"@types/react": "^18.2.23",
"typescript": "^5.4.2"
}
},
"packageManager": "yarn@4.15.0"
}
56 changes: 30 additions & 26 deletions src/binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export type Binding<T = BindingMeta> = BindingBase<T> & {
};
export type ObjectBinding = Binding<ObjectBindingMeta>;

const typeNameMap = {
const typeNameMap: Partial<Record<CType, string>> = {
[CType.Double]: "double",
[CType.Size]: "size_t",
[CType.Int]: "int",
Expand All @@ -137,9 +137,9 @@ export function getObjectTypeName(obj: ObjectBinding) {
}
switch (init.kind) {
case SyntaxKind.NumericLiteral:
return typeNameMap[init.type];
return typeNameMap[init.type] || "unknown";
case SyntaxKind.StringLiteral:
return typeNameMap[CType.String];
return typeNameMap[CType.String] || "char*";
case SyntaxKind.NewExpression:
return init.identifier;
default:
Expand Down Expand Up @@ -330,13 +330,9 @@ function compileFunction({
return [
signature,
"{",
formatFuncBody(
locals.map((item) => compiler.compileVariableDeclaration(item))
),
formatFuncBody(locals.map((item) => compileVariableDeclaration(item))),
formatFuncBody(body),
formatFuncBody(
locals.map((item) => compiler.compileObjectDestroyer(item.initializer))
),
formatFuncBody(locals.map((item) => compileObjectDestroyer(item.initializer))),
"}",
]
.filter(Boolean)
Expand All @@ -347,7 +343,7 @@ function compileComponentMethod({
ctx,
name,
args = "",
body = ctx.body,
body,
thatId = "w",
}: {
ctx?: FunctionContext;
Expand All @@ -357,16 +353,17 @@ function compileComponentMethod({
body?: string[];
}) {
const className = getComponentContext().name;
const methodBody = [
`${className}_react_t *_that = ui_widget_get_data(${thatId}, ${className}_proto)`,
...(body || ctx?.body || []),
ctx?.hasStateOperation ? `${className}_react_update(${thatId})` : undefined,
].filter((line): line is string => Boolean(line));
return compileFunction({
locals: ctx?.locals || [],
signature: `static void ${className}_${
name || ctx?.name || "unnamed_func"
}(ui_widget_t *w${args})`,
body: [
`${className}_react_t *_that = ui_widget_get_data(${thatId}, ${className}_proto)`,
...(body || []),
ctx?.hasStateOperation && `${className}_react_update(${thatId})`,
],
body: methodBody,
});
}

Expand Down Expand Up @@ -492,8 +489,8 @@ export function pushFunctionComponent(ctx: FunctionContext) {
contextList.push(ctx);
}

export function popFunctionComponent(ctx: FunctionContext) {
contextList.push(ctx);
export function popFunctionComponent() {
contextList.pop();
}

export function setComponentContext(ctx: ComponentContext) {
Expand All @@ -515,13 +512,16 @@ function createStringLiteral(value: string | null = null): StringLiteral {
};
}

function createBinding(meta: BindingMeta, data: Record<string, any> = {}) {
function createBinding<T extends object>(
meta: BindingMeta,
data: T = {} as T
) {
const binding = new Proxy(
{ __meta__: meta, ...data },
{ __meta__: meta, ...data } as Record<string | symbol, unknown>,
{
get(target, p, receiver) {
if (p in target) {
return target[p];
return Reflect.get(target, p, receiver);
}
if (typeof p !== "string") {
return null;
Expand All @@ -533,7 +533,7 @@ function createBinding(meta: BindingMeta, data: Record<string, any> = {}) {
name: p,
});
},
construct(target, args) {
construct(target: { __meta__: BindingMeta }, args) {
if (target.__meta__.kind !== BindingKind.Object) {
throw new SyntaxError("Module cannot be used as a constructor");
}
Expand All @@ -542,28 +542,32 @@ function createBinding(meta: BindingMeta, data: Record<string, any> = {}) {
}
return createVariable(meta.name, args);
},
apply(target: Binding, _thisArg, args) {
apply(target: { __meta__: BindingMeta }, _thisArg, args) {
if (target.__meta__.kind === BindingKind.Module) {
throw new SyntaxError("Module cannot be used as a function");
}
const ctx = getFunctionContext();
ctx.body.push(
compileCallExpression(
resolveBindingIdentify(target as ObjectBinding),
resolveBindingIdentify(target as unknown as ObjectBinding),
args
)
);
return undefined;
},
}
) as Binding;
) as unknown as Binding & T;
return binding;
}

function createObjectBinding(meta: Omit<ObjectBindingMeta, "kind">, data = {}) {
function createObjectBinding<T extends object = {}>(
meta: Omit<ObjectBindingMeta, "kind">,
data: T = {} as T
): ObjectBinding & T {
return createBinding(
{ ...meta, kind: BindingKind.Object },
data
) as ObjectBinding;
) as ObjectBinding & T;
}

export function isObjectBinding(val: any): val is ObjectBinding {
Expand Down
Loading