js bindings for builtins/web/node/deno/bun (browser APIs split to mizchi/js_browser)
Dependencies
moon 0.1.20251209 (8d6e473 2025-12-09)
moonc v0.6.34+7262739a4-nightly (2025-12-09)
moonrun 0.1.20251209 (8d6e473 2025-12-09)$ moon add mizchi/js{
"import": ["mizchi/js/core", "mizchi/js"]
}⚠️ Future Plans: Platform-specific APIs (Node.js, Browser, Deno) will be split into separate packages in the future. The core mizchi/js package will focus on JavaScript built-ins and Web Standard APIs.Note: Cloudflare Workers bindings have been moved to a separate package: @mizchi/cloudflare-mbt
| Platform | Documentation | Examples | Status |
|---|---|---|---|
| Core JavaScript | src/README.md | js_examples.mbt.md | 🧪 Tested |
| Browser | src/browser/README.md | browser_examples.mbt.md | 🧪 Tested |
| Node.js | src/node/README.md | node_examples.mbt.md | 🧪 Tested |
| Deno | src/deno/README.md | - | 🧪 Tested |
| React | mizchi/npm_typed | See npm_typed repo | 📦 Moved |
| Category | Package | Status | Note |
|---|---|---|---|
| Core FFI & Objects | |||
| Core FFI | mizchi/js/core | 🧪 Tested | get, set, call, etc. |
| Object | mizchi/js/builtins/object | 🧪 Tested | Object manipulation |
| Function | mizchi/js/builtins/function | 🧪 Tested | Function operations |
| Promise | mizchi/js/core | 🧪 Tested | Async/Promise API |
| Error | mizchi/js/builtins/error | 🧪 Tested | Error handling |
| JSON | mizchi/js/builtins/json | 🧪 Tested | JSON parse/stringify |
| Iterator | mizchi/js/builtins/iterator | 🧪 Tested | JS Iterator protocol |
| AsyncIterator | mizchi/js/builtins/iterator | 🧪 Tested | Async iteration |
| WeakMap/Set/Ref | mizchi/js/builtins/weak | 🧪 Tested | Weak references |
| Async Helpers | |||
| run_async | mizchi/js/core | 🧪 Tested | Async execution |
| suspend | mizchi/js/core | 🧪 Tested | Promise suspension |
| sleep | mizchi/js/core | 🧪 Tested | Delay execution |
| promisify | mizchi/js/core | 🧪 Tested | Callback → Promise |
| Category | Package | Status | Note |
|---|---|---|---|
| Global Functions | |||
| Global | mizchi/js/builtins/global | 🧪 Tested | globalThis, parseInt, parseFloat, setTimeout etc. |
| Core Types | |||
| Object | mizchi/js/builtins/object | 🧪 Tested | Object manipulation |
| Function | mizchi/js/builtins/function | 🧪 Tested | Function operations |
| Symbol | mizchi/js/builtins/symbol | 🧪 Tested | Symbol primitive |
| Error | mizchi/js/builtins/error | 🧪 Tested | Error types (TypeError, RangeError, etc.) |
| Primitives & Data | |||
| String | mizchi/js/builtins/string | 🧪 Tested | JsString (String methods) |
| Array | mizchi/js/builtins/array | 🧪 Tested | JsArray (Array methods) |
| BigInt | mizchi/js/builtins/bigint | 🧪 Tested | JsBigInt (arbitrary precision) |
| JSON | mizchi/js/builtins/json | 🧪 Tested | JSON parse/stringify |
| Date & Math | |||
| Date | mizchi/js/builtins/date | 🧪 Tested | Date/time operations |
| Math | mizchi/js/builtins/math | 🧪 Tested | Math operations |
| Collections | |||
| Map/Set | mizchi/js/builtins/collection | 🧪 Tested | JsMap, JsSet |
| WeakMap/Set/Ref | mizchi/js/builtins/weak | 🧪 Tested | WeakMap, WeakSet, WeakRef, FinalizationRegistry |
| Binary Data | |||
| ArrayBuffer | mizchi/js/builtins/arraybuffer | 🧪 Tested | Binary buffers |
| DataView | mizchi/js/builtins/arraybuffer | 🧪 Tested | Buffer views |
See mizchi/js/web for detailed Web APIs documentation
| Category | Package | Status | Note |
|---|---|---|---|
| Console | mizchi/js/web/console | 🧪 Tested | console.log, console.error, etc. |
| fetch | mizchi/js/web/http | 🧪 Tested | HTTP requests |
| Request | mizchi/js/web/http | 🧪 Tested | Request objects |
| Response | mizchi/js/web/http | 🧪 Tested | Response objects |
| Headers | mizchi/js/web/http | 🧪 Tested | HTTP headers |
| FormData | mizchi/js/web/http | 🧪 Tested | Form data |
| URL | mizchi/js/web/url | 🧪 Tested | URL parsing |
| URLSearchParams | mizchi/js/web/url | 🧪 Tested | Query strings |
| URLPattern | mizchi/js/web/url | 🧪 Tested | URL pattern matching |
| Blob | mizchi/js/web/blob | 🧪 Tested | Binary data |
| ReadableStream | mizchi/js/web/streams | 🧪 Tested | Stream reading |
| WritableStream | mizchi/js/web/streams | 🧪 Tested | Stream writing |
| TransformStream | mizchi/js/web/streams | 🧪 Tested | Stream transformation |
| CompressionStream | mizchi/js/web/streams | 🧪 Tested | GZIP/Deflate compression |
| DecompressionStream | mizchi/js/web/streams | 🧪 Tested | GZIP/Deflate decompression |
| TextEncoder | mizchi/js/web/encoding | 🧪 Tested | String to Uint8Array |
| TextDecoder | mizchi/js/web/encoding | 🧪 Tested | Uint8Array to String |
| Event | mizchi/js/web/event | 🧪 Tested | Event objects |
| CustomEvent | mizchi/js/web/event | 🧪 Tested | Custom events |
| MessageEvent | mizchi/js/web/event | 🧪 Tested | Message events |
| Crypto | mizchi/js/web/crypto | 🧪 Tested | Web Crypto API |
| WebSocket | mizchi/js/web/websocket | 🧪 Tested | WebSocket API |
| Worker | mizchi/js/web/worker | 🧪 Tested | Web Workers |
| MessageChannel | mizchi/js/web/message | 🧪 Tested | Message passing |
| MessagePort | mizchi/js/web/message | 🧪 Tested | Message ports |
| WebAssembly | mizchi/js/web/webassembly | 🤖 AI Generated | WASM integration |
| Performance | mizchi/js/web/performance | 🤖 AI Generated | Performance API |
| Platform | Package | Status | Documentation |
|---|---|---|---|
| Node.js | mizchi/js/node/* | 🧪 Tested | Node.js README |
| Browser API | mizchi/js/browser/* | 🧪 Tested | Browser README |
| Deno | mizchi/js/deno | 🧪 Tested | Deno README |
Moved to separate repository: NPM package bindings are now maintained at mizchi/npm_typed
| Category | Packages | Repository |
|---|---|---|
| UI Frameworks | React, React DOM, React Router, Preact, Ink | mizchi/npm_typed |
| Web Frameworks | Hono, better-auth | mizchi/npm_typed |
| AI / LLM | Vercel AI SDK, MCP SDK, Claude Code SDK | mizchi/npm_typed |
| Cloud Services | @aws-sdk/client-s3 (S3, R2, GCS, MinIO) | mizchi/npm_typed |
| Database | PGlite, DuckDB, Drizzle, pg | mizchi/npm_typed |
| Validation | Zod, AJV | mizchi/npm_typed |
| Build Tools | Terser, Vite, Unplugin, Lighthouse | mizchi/npm_typed |
| Utilities | date-fns, semver, chalk, dotenv, chokidar, yargs, debug | mizchi/npm_typed |
| Testing | Testing Library, Puppeteer, Playwright, Vitest, JSDOM, MSW | mizchi/npm_typed |
| Parsing | htmlparser2, js-yaml | mizchi/npm_typed |
| Other | simple-git, ignore, memfs, source-map, comlink | mizchi/npm_typed |
| Feature | Status | Note |
|---|---|---|
| eval() | ❌ Not Supported | Security and type safety concerns |
| new Function() | ❌ Not Supported | Security and type safety concerns |
// Create JavaScript objects
let obj = @js.from_entries([
("name", @js.any("Alice")),
("age", @js.any(30))
])
// Get property
let name = obj["name"]
// Set property
obj["age"] = @js.any(31)
// Call method
let result = obj._call("toString", [])
// Type casting
let age: Int = obj["age"].cast()moon add mizchi/js{
"import": ["mizchi/js"]
}///|
test {
// Create objects
let obj = @core.new_object()
obj["name"] = @core.any("Alice")
obj["age"] = @core.any(25)
// Get properties
let name : String = obj["name"].cast()
let age : Int = obj["age"].cast()
inspect(name, content="Alice")
inspect(age, content="25")
// Call methods
let has_name : Bool = obj._call("hasOwnProperty", [@core.any("name")]).cast()
inspect(has_name, content="true")
// Create from entries
let obj2 = @core.from_entries([("x", @core.any(10)), ("y", @core.any(20))])
let x : Int = obj2["x"].cast()
inspect(x, content="10")
}///|
test {
// Using cast() method (recommended)
let js_value : @core.Any = @core.any(42)
let value : Int = js_value.cast()
inspect(value, content="42")
// Using identity() for low-level FFI
let value2 : Int = @core.identity(js_value)
inspect(value2, content="42")
// Optional types
let nullable : Int? = @core.identity_option(@core.null())
inspect(nullable, content="None")
let some_value : Int? = @core.identity_option(@core.any(100))
inspect(some_value, content="Some(100)")
}///|
test {
// Parse JSON
let json_str = "{\"name\":\"Alice\",\"age\":30}"
let obj = @core.json_parse(json_str)
let name : String = obj["name"].cast()
inspect(name, content="Alice")
// Stringify
let obj2 = @core.from_entries([("x", @core.any(1))])
let json_str2 = @core.json_stringify(obj2)
inspect(json_str2, content="{\"x\":1}")
}///|
test {
// Check nullish values
let null_val = @core.null()
let undefined_val = @core.undefined()
inspect(@core.is_nullish(null_val), content="true")
inspect(@core.is_nullish(undefined_val), content="true")
inspect(@core.is_nullish(@core.any(42)), content="false")
// identity_option for safe conversion
let maybe_null : Int? = @core.identity_option(null_val)
inspect(maybe_null, content="None")
// from_option for MoonBit Option -> JS
let js_some = @core.from_option(Some(42))
let js_none = @core.from_option((None : Int?))
inspect(@core.is_nullish(js_some), content="false")
inspect(@core.is_nullish(js_none), content="true")
}///|
test {
// @core.Any can hold any JavaScript value
let num : @core.Any = @core.any(42)
let str : @core.Any = @core.any("hello")
let obj : @core.Any = @core.new_object()
inspect(@core.typeof_(num), content="number")
inspect(@core.typeof_(str), content="string")
inspect(@core.typeof_(obj), content="object")
}///|
test {
// Convert MoonBit values to JavaScript
let js_int = @core.any(42)
let js_str = @core.any("hello")
let js_bool = @core.any(true)
let int_val : Int = js_int.cast()
let str_val : String = js_str.cast()
let bool_val : Bool = js_bool.cast()
inspect(int_val, content="42")
inspect(str_val, content="hello")
inspect(bool_val, content="true")
}///|
test {
let obj = @core.from_entries([
("name", @core.any("Alice")),
("age", @core.any(30)),
])
// Using cast()
let name : String = obj["name"].cast()
let age : Int = obj["age"].cast()
inspect(name, content="Alice")
inspect(age, content="30")
}///|
test {
let obj = @core.from_entries([("name", @core.any("Alice"))])
// Existing field
let name : String? = @core.identity_option(obj["name"])
inspect(
name,
content=(
#|Some("Alice")
),
)
// Missing field returns undefined
let missing : String? = @core.identity_option(obj["missing"])
inspect(missing, content="None")
}///|
test {
let arr = @core.any([1, 2, 3])
// Call method with arguments
let joined : String = arr._call("join", [@core.any("-")]).cast()
inspect(joined, content="1-2-3")
// Get property
let length : Int = arr["length"].cast()
inspect(length, content="3")
}///|
extern "js" fn get_array_constructor() -> @core.TypeOf[Array[Int]] =
#| () => Array
///|
test "TypeOf usage" {
let arr_ctor = get_array_constructor()
// Create instance using constructor
let arr : Array[Int] = @core.new_instance(arr_ctor, [])
inspect(arr.length(), content="0")
// instanceof check
let js_arr = @core.any([1, 2, 3])
inspect(arr_ctor.is_instanceof(js_arr), content="true")
inspect(arr_ctor.is_instanceof(@core.any("not array")), content="false")
}| Function/Type | Purpose | Example |
|---|---|---|
| @core.Any | Universal JS value type | let v : @core.Any = ... |
| @core.any(x) | MoonBit -> JS conversion | @core.any(42) |
| @core.identity(x) | Unsafe type cast | let n : Int = @core.identity(v) |
| .cast() | Same as identity (method) | v.cast() |
| ._get(key) / [key] | Property access | obj["name"] |
| ._set(key, val) / [key]= | Property assignment | obj["age"] = @core.any(30) |
| ._call(method, args) | Method call | obj._call("toString", []) |
| TypeOf[T] | Constructor type | TypeOf[Date] |
fn decodeURIComponent(encoded_str : String) -> Stringlet p : Promise[Int] = from_async(async fn() {
let result = some_async_operation()
result
})fn parseFloat(string : String) -> Double?fn parseInt(string : String, radix? : Int) -> Int?fn queueMicrotask(callback : () -> Unit) -> Unitasync fn[T, E : Error] suspend(f : ((T) -> Unit, (E) -> Unit) -> Unit) -> T raise Elet result = throwable(() => undefined()._invoke([]))js bindings for builtins/web/node/deno/bun (browser APIs split to mizchi/js_browser)
Dependencies