- Notifications
You must be signed in to change notification settings - Fork0
mizchi/js.mbt
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Comprehensive JavaScript/ FFI bindings for MoonBit, supporting multiple runtimes and platforms.
v0.10.0 requires MoonBit nightly2025-12-09 or later for ESM#module directive support:
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)If you need stable toolchain compatibility, usev0.8.x.
NPM package bindings have moved: Allmizchi/js/npm/* packages are now in a separate repository:mizchi/npm_typed
$ moon add mizchi/js
Add to yourmoon.pkg.json:
{"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 coremizchi/jspackage 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 |
- MoonBit Cheatsheet - Quick reference for MoonBit syntax
- FFI Best Practice - Best practice for MoonBit JavaScript FFI
- Escape Hatch Pattern - Advanced FFI techniques
- For TypeScript Users - Migration guide from TypeScript
- 🧪Tested: Comprehensive test coverage, production ready
- 🚧Partially: Core functionality implemented, tests incomplete
- 🤖AI Generated: FFI bindings created, needs testing
- 📅Planned: Scheduled for future implementation
- ❌Not Supported: Technical limitations
Themizchi/js/core package provides the foundation for JavaScript interoperability in MoonBit:
Type System
Any- Opaque type for JavaScript valuesNullable[T]- Representsnull | TNullish[T]- Representsnull | undefined | TUnion2[A,B]~Union5[A,B,C,D,E]- TypeScript union types (A | B)Promise[T]- JavaScript Promise wrapper
FFI Operations (zero-cost conversions)
identity[A,B](value: A) -> B- Type casting using%identityany[T](value: T) -> Any- Convert to AnyAny::cast[T](self) -> T- Cast from Anyobj["key"],obj["key"] = value- Property access (or_get(key),_set(key, value))Any::_call(method, args),Any::_invoke(args)- Method calls
Object & JSON
new_object(),new_array()- Create JS objects/arraysobject_keys(),object_values(),object_assign(),object_has_own()json_stringify(),json_parse(),json_stringify_pretty()
Async/Promise Support
run_async(f)- Execute async functions (MoonBit builtin%async.run)suspend(f)- Await promises (MoonBit builtin%async.suspend)promisify0~promisify3- Convert callbacks to promises- Promise utilities:
resolve,reject,all,race,any,withResolvers
Error Handling
JsError- Generic JS error typeThrowError- Wrapper for thrown errorstry_sync(op)- Safe wrapper converting JS exceptions to MoonBit errorsthrowable(f)- Convert JS exceptions to ThrowErrorexport_sync(op)- Convert MoonBit errors to JS exceptionsthrow_error(msg)- Throw JS Error
Type Checking
is_object(),is_array(),is_null(),is_undefined(),is_nullish()
Nullish Utilities
Nullish::to_option(),Nullable::to_option()- Convert to MoonBit Optionnullable(opt)- Convert Option to JS nullableas_any(opt)- Convert Option[Any] to Any
| 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 |
All JavaScript built-in objects are exported frommizchi/js:
| 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 |
| memory | |||
| Pattern & Reflection | |||
| RegExp | mizchi/js/builtins/regexp | 🧪 Tested | Regular expressions |
| Reflect | mizchi/js/builtins/reflect | 🧪 Tested | Reflection API |
| Proxy | mizchi/js/builtins/proxy | 🤖 AI Generated | Proxy API |
| Iteration & Async | |||
| Iterator | mizchi/js/builtins/iterator | 🧪 Tested | JsIterator protocol |
| AsyncIterator | mizchi/js/builtins/iterator | 🧪 Tested | Async iteration |
| Concurrency | |||
| Atomics | mizchi/js/builtins/atomics | 🧪 Tested | Atomic operations |
| Resource Management | |||
| DisposableStack | mizchi/js/builtins/disposable | 🧪 Tested | Disposable resources |
Platform-independent Web Standard APIs (browsers, Node.js, Deno, edge runtimes):
Seemizchi/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 atmizchi/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 |
- ✅Node.js Core APIs -
fs,path,process,child_process, etc. - ✅Deno Runtime - File system, permissions, testing
- ✅Bun Runtime - Process, hashing, glob, file operations
- ✅DOM APIs - Full browser DOM manipulation
- ✅Web Standard APIs - fetch, URL, Streams, Crypto, WebSocket
- 📦React/NPM Packages - Moved tomizchi/npm_typed
- 📦Cloudflare Workers - Moved tomizchi/cloudflare.mbt
- Provide comprehensive JavaScript FFI bindings for MoonBit
- Platform Coverage
- ✅ Browser DOM and Web APIs
- ✅ Node.js/Deno/Bun runtime support
- ✅ JavaScript built-in objects and Web Standard APIs
- Ecosystem
- 📦 NPM package bindings:mizchi/npm_typed
- 📦 Cloudflare Workers:mizchi/cloudflare.mbt
// Create JavaScript objectsletobj=@js.from_entries([ ("name",@js.any("Alice")), ("age",@js.any(30))])// Get propertyletname=obj["name"]// Set propertyobj["age"]=@js.any(31)// Call methodletresult=obj._call("toString", [])// Type castingletage:Int=obj["age"].cast()
MIT
About
Moonbit Js bindings
Resources
Contributing
Uh oh!
There was an error while loading.Please reload this page.