Skip to content

Commit 0b4dac9

Browse files
committed
Basic test module
1 parent 165bb60 commit 0b4dac9

File tree

8 files changed

+105
-13
lines changed

8 files changed

+105
-13
lines changed

crates/bindings-typescript/src/lib/binary_reader.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,6 @@ export default class BinaryReader {
158158

159159
readString(): string {
160160
const uint8Array = this.readUInt8Array();
161-
const decoder = new TextDecoder('utf-8');
162-
const value = decoder.decode(uint8Array);
163-
this.#offset += length;
164-
return value;
161+
return new TextDecoder('utf-8').decode(uint8Array);
165162
}
166163
}

crates/bindings-typescript/src/server/rt.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,13 @@ const _syscalls = () => ({
146146
});
147147

148148
const sys = {} as ReturnType<typeof _syscalls>;
149+
function initSys() {
150+
if (Object.isFrozen(sys)) return;
151+
for (const [name, syscall] of Object.entries(_syscalls())) {
152+
(sys as any)[name] = wrapSyscall(syscall);
153+
}
154+
freeze(sys);
155+
}
149156

150157
globalThis.__call_reducer__ = function __call_reducer__(
151158
reducer_id,
@@ -154,6 +161,7 @@ globalThis.__call_reducer__ = function __call_reducer__(
154161
timestamp,
155162
args_buf
156163
) {
164+
initSys();
157165
const args_type = AlgebraicType.Product(
158166
MODULE_DEF.reducers[reducer_id].params
159167
);
@@ -171,11 +179,7 @@ globalThis.__call_reducer__ = function __call_reducer__(
171179
};
172180

173181
globalThis.__describe_module__ = function __describe_module__() {
174-
for (const [name, syscall] of Object.entries(_syscalls())) {
175-
(sys as any)[name] = wrapSyscall(syscall);
176-
}
177-
freeze(sys);
178-
182+
initSys();
179183
return RawModuleDef.V9(MODULE_DEF);
180184
};
181185

crates/core/src/host/v8/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,12 +421,12 @@ pub(crate) fn with_scope<R>(
421421
logic(scope)
422422
};
423423

424-
let timeout_thread_cancel_flag = run_reducer_timeout(callback_every, callback, budget, isolate_handle);
424+
// let timeout_thread_cancel_flag = run_reducer_timeout(callback_every, callback, budget, isolate_handle);
425425

426426
let ret = with_isolate(&mut isolate);
427427

428428
// Cancel the execution timeout in `run_reducer_timeout`.
429-
timeout_thread_cancel_flag.store(true, Ordering::Relaxed);
429+
// timeout_thread_cancel_flag.store(true, Ordering::Relaxed);
430430

431431
(isolate, ret)
432432
}
@@ -542,7 +542,7 @@ fn call_call_reducer(
542542
let sender = serialize_to_js(scope, &sender.to_u256())?;
543543
let conn_id: v8::Local<'_, v8::Value> = serialize_to_js(scope, &conn_id.to_u128())?;
544544
let timestamp = serialize_to_js(scope, &timestamp)?;
545-
let reducer_args = serialize_to_js(scope, &reducer_args.tuple.elements)?;
545+
let reducer_args = serialize_to_js(scope, reducer_args.get_bsatn())?;
546546
let args = &[reducer_id, sender, conn_id, timestamp, reducer_args];
547547

548548
// Get the function on the global proxy object and convert to a function.

modules/module-test-ts/index.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import 'fast-text-encoding';
2+
import { schema, table, t } from '../../sdks/typescript/src/server';
3+
4+
const spacetime = schema(
5+
table(
6+
{
7+
name: 'person',
8+
public: true,
9+
indexes: [{ name: 'age', algorithm: 'btree', columns: ['age'] }],
10+
},
11+
{
12+
id: t.u32().primaryKey().autoInc(),
13+
name: t.string(),
14+
age: t.u8(),
15+
}
16+
)
17+
);
18+
19+
spacetime.reducer(
20+
'add',
21+
{ name: t.string(), age: t.u8() },
22+
(ctx, { name, age }) => {
23+
ctx.db.person.insert({ id: 0, name, age });
24+
}
25+
);
26+
27+
spacetime.reducer('say_hello', {}, ctx => {
28+
for (const person of ctx.db.person.iter()) {
29+
console.log(`Hello, ${person.name}}!`);
30+
}
31+
console.log('Hello, World!');
32+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "sdk-test-module",
3+
"license": "ISC",
4+
"type": "module",
5+
"sideEffects": false,
6+
"scripts": {
7+
"build": "tsup"
8+
},
9+
"dependencies": {
10+
"fast-text-encoding": "^1.0.0"
11+
},
12+
"devDependencies": {
13+
"tsup": "^8.1.0"
14+
}
15+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// tsup.config.ts
2+
import { defineConfig, type Options } from 'tsup';
3+
4+
const outExtension = (ctx: { format: string }) => ({
5+
js: ctx.format === 'cjs' ? '.cjs' : ctx.format === 'esm' ? '.mjs' : '.js',
6+
});
7+
8+
export default defineConfig([
9+
{
10+
entry: { index: 'index.ts' },
11+
format: ['esm'],
12+
target: 'es2022',
13+
outDir: 'dist',
14+
dts: false,
15+
sourcemap: true,
16+
clean: true,
17+
platform: 'neutral', // flip to 'node' if you actually rely on Node builtins
18+
treeshake: 'smallest',
19+
external: ['undici'],
20+
noExternal: ['base64-js', 'fast-text-encoding'],
21+
outExtension,
22+
},
23+
]) satisfies
24+
| Options
25+
| Options[]
26+
| ((
27+
overrideOptions: Options
28+
) => Options | Options[] | Promise<Options | Options[]>);

pnpm-lock.yaml

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pnpm-workspace.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ packages:
22
- 'crates/bindings-typescript'
33
- 'crates/bindings-typescript/test-app'
44
- 'crates/bindings-typescript/examples/quickstart-chat'
5-
- 'docs'
5+
- 'modules/module-test-ts'
6+
- 'docs'

0 commit comments

Comments
 (0)