Skip to content

Commit 20c634a

Browse files
authored
Split runtime into multiple files (#150)
1 parent 53a2de6 commit 20c634a

File tree

13 files changed

+957
-557
lines changed

13 files changed

+957
-557
lines changed

Runtime/rollup.config.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import typescript from "@rollup/plugin-typescript";
2+
3+
/** @type {import('rollup').RollupOptions} */
4+
const config = {
5+
input: "src/index.ts",
6+
output: [
7+
{
8+
file: "lib/index.mjs",
9+
format: "esm",
10+
},
11+
{
12+
dir: "lib",
13+
format: "umd",
14+
name: "JavaScriptKit",
15+
},
16+
],
17+
plugins: [typescript()],
18+
};
19+
20+
export default config;

Runtime/src/closure-heap.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { ExportedFunctions } from "./types";
2+
3+
/// Memory lifetime of closures in Swift are managed by Swift side
4+
export class SwiftClosureDeallocator {
5+
private functionRegistry: FinalizationRegistry<number>;
6+
7+
constructor(exports: ExportedFunctions) {
8+
if (typeof FinalizationRegistry === "undefined") {
9+
throw new Error(
10+
"The Swift part of JavaScriptKit was configured to require " +
11+
"the availability of JavaScript WeakRefs. Please build " +
12+
"with `-Xswiftc -DJAVASCRIPTKIT_WITHOUT_WEAKREFS` to " +
13+
"disable features that use WeakRefs."
14+
);
15+
}
16+
17+
this.functionRegistry = new FinalizationRegistry((id) => {
18+
exports.swjs_free_host_function(id);
19+
});
20+
}
21+
22+
track(func: Function, func_ref: number) {
23+
this.functionRegistry.register(func, func_ref);
24+
}
25+
}

Runtime/src/find-global.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
interface GlobalVariable {}
2+
declare const global: GlobalVariable;
3+
4+
export let globalVariable: any;
5+
if (typeof globalThis !== "undefined") {
6+
globalVariable = globalThis;
7+
} else if (typeof window !== "undefined") {
8+
globalVariable = window;
9+
} else if (typeof global !== "undefined") {
10+
globalVariable = global;
11+
} else if (typeof self !== "undefined") {
12+
globalVariable = self;
13+
}

0 commit comments

Comments
 (0)