Compare commits
201 Commits
misty
...
mach_suite
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b771b2b5d8 | ||
|
|
68fb440502 | ||
|
|
e7a2f16004 | ||
|
|
3a8a17ab60 | ||
|
|
8a84be65e1 | ||
|
|
c1910ee1db | ||
|
|
7036cdf2d1 | ||
|
|
fbeec17ce5 | ||
|
|
2c55ae8cb2 | ||
|
|
259bc139fc | ||
|
|
a252412eca | ||
|
|
b327e16463 | ||
|
|
da6f096a56 | ||
|
|
1320ef9f47 | ||
|
|
ed4a5474d5 | ||
|
|
f52dd80d52 | ||
|
|
504e268b9d | ||
|
|
0d47002167 | ||
|
|
b65db63447 | ||
|
|
c1ccff5437 | ||
|
|
2f681fa366 | ||
|
|
682b1cf9cf | ||
|
|
ddf3fc1c77 | ||
|
|
f1a5072ff2 | ||
|
|
f44fb502be | ||
|
|
d75ce916d7 | ||
|
|
fe5dc6ecc9 | ||
|
|
54673e4a04 | ||
|
|
0d8b5cfb04 | ||
|
|
3d71f4a363 | ||
|
|
4deb0e2577 | ||
|
|
67b96e1627 | ||
|
|
4e5f1d8faa | ||
|
|
bd577712d9 | ||
|
|
6df3b741cf | ||
|
|
178837b88d | ||
|
|
120ce9d30c | ||
|
|
58f185b379 | ||
|
|
f7b5252044 | ||
|
|
ded5f7d74b | ||
|
|
fe6033d6cb | ||
|
|
60e61eef76 | ||
|
|
ad863fb89b | ||
|
|
96f8157039 | ||
|
|
c4ff0bc109 | ||
|
|
877250b1d8 | ||
|
|
747227de40 | ||
|
|
3f7e34cd7a | ||
|
|
cef5c50169 | ||
|
|
0428424ec7 | ||
|
|
78e64c5067 | ||
|
|
ff11c49c39 | ||
|
|
b8b110b616 | ||
|
|
930dcfba36 | ||
|
|
eeccb3b34a | ||
|
|
407797881c | ||
|
|
7069475729 | ||
|
|
3e42c57479 | ||
|
|
4b76728230 | ||
|
|
4ff9332d38 | ||
|
|
27e852af5b | ||
|
|
66a44595c8 | ||
|
|
fc0a1547dc | ||
|
|
c0b4e70eb2 | ||
|
|
f4714b2b36 | ||
|
|
7f691fd52b | ||
|
|
d5209e1d59 | ||
|
|
68e2395b92 | ||
|
|
1b747720b7 | ||
|
|
849123d8fc | ||
|
|
6ad919624b | ||
|
|
a11f3e7d47 | ||
|
|
3d1fd37979 | ||
|
|
8fc9bfe013 | ||
|
|
368511f666 | ||
|
|
3934cdb683 | ||
|
|
45556c344d | ||
|
|
bc87fe5f70 | ||
|
|
790293d915 | ||
|
|
872cd6ab51 | ||
|
|
e04ab4c30c | ||
|
|
0503acb7e6 | ||
|
|
d0c68d7a7d | ||
|
|
7469383e66 | ||
|
|
1fee8f9f8b | ||
|
|
a4f3b025c5 | ||
|
|
d18ea1b330 | ||
|
|
4de0659474 | ||
|
|
27a9b72b07 | ||
|
|
a3622bd5bd | ||
|
|
2f6700415e | ||
|
|
243d92f7f3 | ||
|
|
8f9d026b9b | ||
|
|
2c9ac8f7b6 | ||
|
|
80f24e131f | ||
|
|
a8f8af7662 | ||
|
|
f5b3494762 | ||
|
|
13a6f6c79d | ||
|
|
1a925371d3 | ||
|
|
08d2bacb1f | ||
|
|
7322153e57 | ||
|
|
cc72c4cb0f | ||
|
|
ae1f09a28f | ||
|
|
3c842912a1 | ||
|
|
7cacf32078 | ||
|
|
b740612761 | ||
|
|
6001c2b4bb | ||
|
|
98625fa15b | ||
|
|
87fafa44c8 | ||
|
|
45ce76aef7 | ||
|
|
32fb44857c | ||
|
|
31d67f6710 | ||
|
|
bae4e957e9 | ||
|
|
3621b1ef33 | ||
|
|
836227c8d3 | ||
|
|
0ae59705d4 | ||
|
|
8e2607b6ca | ||
|
|
dc73e86d8c | ||
|
|
555cceb9d6 | ||
|
|
fbb7933eb6 | ||
|
|
0287d6ada4 | ||
|
|
73cd6a255d | ||
|
|
83ea67c01b | ||
|
|
16059cca4e | ||
|
|
9ffe60ebef | ||
|
|
2beafec5d9 | ||
|
|
aba8eb66bd | ||
|
|
1abcaa92c7 | ||
|
|
168f7c71d5 | ||
|
|
56ed895b6e | ||
|
|
1e4646999d | ||
|
|
68d6c907fe | ||
|
|
8150c64c7d | ||
|
|
024d796ca4 | ||
|
|
ea185dbffd | ||
|
|
6571262af0 | ||
|
|
77ae133747 | ||
|
|
142a2d518b | ||
|
|
5b65c64fe5 | ||
|
|
e985fa5fe1 | ||
|
|
160ade2410 | ||
|
|
e2bc5948c1 | ||
|
|
8cf98d8a9e | ||
|
|
3c38e828e5 | ||
|
|
af2d296f40 | ||
|
|
0a45394689 | ||
|
|
32885a422f | ||
|
|
8959e53303 | ||
|
|
8a9a02b131 | ||
|
|
f9d68b2990 | ||
|
|
017a57b1eb | ||
|
|
ff8c68d01c | ||
|
|
9212003401 | ||
|
|
f9f8a4db42 | ||
|
|
8db95c654b | ||
|
|
63feabed5d | ||
|
|
c814c0e1d8 | ||
|
|
bead0c48d4 | ||
|
|
98dcab4ba7 | ||
|
|
ae44ce7b4b | ||
|
|
1c38699b5a | ||
|
|
9a70a12d82 | ||
|
|
a8a271e014 | ||
|
|
91761c03e6 | ||
|
|
5a479cc765 | ||
|
|
97a003e025 | ||
|
|
20f14abd17 | ||
|
|
19ba184fec | ||
|
|
7909b11f6b | ||
|
|
27229c675c | ||
|
|
64d234ee35 | ||
|
|
e861d73eec | ||
|
|
a24331aae5 | ||
|
|
c1cb922b64 | ||
|
|
aacb0b48bf | ||
|
|
b38aec95b6 | ||
|
|
b29d3c2fe0 | ||
|
|
1cc3005b68 | ||
|
|
b86cd042fc | ||
|
|
8b7af0c22a | ||
|
|
f71f6a296b | ||
|
|
9bd764b11b | ||
|
|
058cdfd2e4 | ||
|
|
1ef837c6ff | ||
|
|
cd21de3d70 | ||
|
|
a98faa4dbb | ||
|
|
08559234c4 | ||
|
|
c3dc27eac6 | ||
|
|
7170a9c7eb | ||
|
|
a08ee50f84 | ||
|
|
ed7dd91c3f | ||
|
|
3abe20fee0 | ||
|
|
a92a96118e | ||
|
|
4e407fe301 | ||
|
|
ab74cdc173 | ||
|
|
2c9d039271 | ||
|
|
80d314c58f | ||
|
|
611fba2b6f | ||
|
|
f5fad52d47 | ||
|
|
2fc7d333ad | ||
|
|
d4635f2a75 |
1
.gitattributes
vendored
Normal file
1
.gitattributes
vendored
Normal file
@@ -0,0 +1 @@
|
||||
*.mach binary merge=ours
|
||||
4
.gitignore
vendored
4
.gitignore
vendored
@@ -1,6 +1,7 @@
|
||||
.git/
|
||||
.obj/
|
||||
website/
|
||||
website/public/
|
||||
website/.hugo_build.lock
|
||||
bin/
|
||||
build/
|
||||
*.zip
|
||||
@@ -15,6 +16,7 @@ build/
|
||||
source/shaders/*.h
|
||||
.DS_Store
|
||||
*.html
|
||||
!website/themes/**/*.html
|
||||
.vscode
|
||||
*.icns
|
||||
icon.ico
|
||||
|
||||
134
CLAUDE.md
134
CLAUDE.md
@@ -1,25 +1,123 @@
|
||||
# Code style
|
||||
All code is done with 2 spaces for indentation.
|
||||
# ƿit (pit) Language Project
|
||||
|
||||
For cell script and its integration files, objects are preferred over classes, and preferrably limited use of prototypes, make objects sendable between actors (.ce files).
|
||||
## Building
|
||||
|
||||
## cell script format
|
||||
Cell script files end in .ce or .cm. Cell script is similar to Javascript but with some differences.
|
||||
Recompile after changes: `make`
|
||||
Bootstrap from scratch (first time): `make bootstrap`
|
||||
Run `cell --help` to see all CLI flags.
|
||||
|
||||
Variables are delcared with 'var'. Var behaves like let.
|
||||
Constants are declared with 'def'.
|
||||
!= and == are strict, there is no !== or ===.
|
||||
There is no undefined, only null.
|
||||
There are no classes, only objects and prototypes.
|
||||
Prefer backticks for string interpolation. Otherwise, convering non strings with the text() function is required.
|
||||
Everything should be lowercase.
|
||||
## Code Style
|
||||
|
||||
There are no arraybuffers, only blobs, which work with bits. They must be stoned like stone(blob) before being read from.
|
||||
All code uses 2 spaces for indentation. K&R style for C and Javascript.
|
||||
|
||||
## c format
|
||||
For cell script integration files, everything should be declared static that can be. Most don't have headers at all. Files in a package are not shared between packages.
|
||||
## ƿit Script Quick Reference
|
||||
|
||||
There is no undefined, so JS_IsNull and JS_NULL should be used only.
|
||||
ƿit script files: `.ce` (actors) and `.cm` (modules). The syntax is similar to JavaScript with important differences listed below.
|
||||
|
||||
## how module loading is done in cell script
|
||||
Within a package, a c file, if using the correct macros (CELL_USE_FUNCS etc), will be loaded as a module with its name; so png.c inside ac package is loaded as <package>/png, giving you access to its functions.
|
||||
### Key Differences from JavaScript
|
||||
|
||||
- `var` (mutable) and `def` (constant) — no `let` or `const`
|
||||
- `==` and `!=` are strict (no `===` or `!==`)
|
||||
- No `undefined` — only `null`
|
||||
- No classes — only objects and prototypes (`meme()`, `proto()`, `isa()`)
|
||||
- No `for...in`, `for...of`, spread (`...`), rest params, or default params
|
||||
- No named function declarations — use `var fn = function() {}` or arrow functions
|
||||
- Variables must be declared at function body level only (not in if/while/for/blocks)
|
||||
- All variables must be initialized at declaration (`var x` alone is an error; use `var x = null`)
|
||||
- No `try`/`catch`/`throw` — use `disrupt`/`disruption`
|
||||
- No arraybuffers — only `blob` (works with bits; must `stone(blob)` before reading)
|
||||
- Identifiers can contain `?` and `!` (e.g., `nil?`, `set!`, `is?valid`)
|
||||
- Prefer backticks for string interpolation; otherwise use `text()` to convert non-strings
|
||||
- Everything should be lowercase
|
||||
|
||||
### Intrinsic Functions (always available, no `use()` needed)
|
||||
|
||||
The creator functions are **polymorphic** — behavior depends on argument types:
|
||||
|
||||
- `array(number)` — create array of size N filled with null
|
||||
- `array(number, value_or_fn)` — create array with initial values
|
||||
- `array(array)` — copy array
|
||||
- `array(array, fn)` — map
|
||||
- `array(array, array)` — concatenate
|
||||
- `array(array, from, to)` — slice
|
||||
- `array(record)` — get keys as array of text
|
||||
- **`array(text)` — split text into individual characters** (e.g., `array("hello")` → `["h","e","l","l","o"]`)
|
||||
- `array(text, separator)` — split by separator
|
||||
- `array(text, length)` — split into chunks of length
|
||||
|
||||
- `text(array, separator)` — join array into text
|
||||
- `text(number)` or `text(number, radix)` — number to text
|
||||
- `text(text, from, to)` — substring
|
||||
|
||||
- `number(text)` or `number(text, radix)` — parse text to number
|
||||
- `number(logical)` — boolean to number
|
||||
|
||||
- `record(record)` — copy
|
||||
- `record(record, another)` — merge
|
||||
- `record(array_of_keys)` — create record from keys
|
||||
|
||||
Other key intrinsics: `length()`, `stone()`, `is_stone()`, `print()`, `filter()`, `find()`, `reduce()`, `sort()`, `reverse()`, `some()`, `every()`, `starts_with()`, `ends_with()`, `meme()`, `proto()`, `isa()`, `splat()`, `apply()`, `extract()`, `replace()`, `search()`, `format()`, `lower()`, `upper()`, `trim()`
|
||||
|
||||
Sensory functions: `is_array()`, `is_text()`, `is_number()`, `is_object()`, `is_function()`, `is_null()`, `is_logical()`, `is_integer()`, `is_stone()`, etc.
|
||||
|
||||
### Standard Library (loaded with `use()`)
|
||||
|
||||
- `blob` — binary data (bits, not bytes)
|
||||
- `time` — time constants and conversions
|
||||
- `math` — trig, logarithms, roots (`math/radians`, `math/turns`)
|
||||
- `json` — JSON encoding/decoding
|
||||
- `random` — random number generation
|
||||
|
||||
### Actor Model
|
||||
|
||||
- `.ce` files are actors (independent execution units, don't return values)
|
||||
- `.cm` files are modules (return a value, cached and frozen)
|
||||
- Actors never share memory; communicate via `$send()` message passing
|
||||
- Actor intrinsics start with `$`: `$me`, `$stop()`, `$send()`, `$start()`, `$delay()`, `$receiver()`, `$clock()`, `$portal()`, `$contact()`, `$couple()`, `$unneeded()`, `$connection()`, `$time_limit()`
|
||||
|
||||
### Requestors (async composition)
|
||||
|
||||
`sequence()`, `parallel()`, `race()`, `fallback()` — compose asynchronous operations. See docs/requestors.md.
|
||||
|
||||
### Error Handling
|
||||
|
||||
```javascript
|
||||
var fn = function() {
|
||||
disrupt // bare keyword, no value
|
||||
} disruption {
|
||||
// handle error; can re-raise with disrupt
|
||||
}
|
||||
```
|
||||
|
||||
### Push/Pop Syntax
|
||||
|
||||
```javascript
|
||||
var a = [1, 2]
|
||||
a[] = 3 // push: [1, 2, 3]
|
||||
var v = a[] // pop: v is 3, a is [1, 2]
|
||||
```
|
||||
|
||||
## C Integration
|
||||
|
||||
- Declare everything `static` that can be
|
||||
- Most files don't have headers; files in a package are not shared between packages
|
||||
- No undefined in C API: use `JS_IsNull` and `JS_NULL` only
|
||||
- A C file with correct macros (`CELL_USE_FUNCS` etc) is loaded as a module by its name (e.g., `png.c` in a package → `use('<package>/png')`)
|
||||
|
||||
## Project Layout
|
||||
|
||||
- `source/` — C source for the cell runtime and CLI
|
||||
- `docs/` — master documentation (Markdown), reflected on the website
|
||||
- `website/` — Hugo site; theme at `website/themes/knr/`
|
||||
- `internal/` — internal ƿit scripts (engine.cm etc.)
|
||||
- `packages/` — core packages
|
||||
- `Makefile` — build system (`make` to rebuild, `make bootstrap` for first build)
|
||||
|
||||
## Documentation
|
||||
|
||||
The `docs/` folder is the single source of truth. The website at `website/` mounts it via Hugo. Key files:
|
||||
- `docs/language.md` — language syntax reference
|
||||
- `docs/functions.md` — all built-in intrinsic functions
|
||||
- `docs/actors.md` — actor model and actor intrinsics
|
||||
- `docs/requestors.md` — async requestor pattern
|
||||
- `docs/library/*.md` — intrinsic type reference (text, number, array, object) and standard library modules
|
||||
|
||||
26
Makefile
26
Makefile
@@ -5,10 +5,16 @@
|
||||
# or manually build with meson once.
|
||||
#
|
||||
# The cell shop is at ~/.cell and core scripts are installed to ~/.cell/core
|
||||
#
|
||||
# See BUILDING.md for details on the bootstrap process and .mach files.
|
||||
|
||||
CELL_SHOP = $(HOME)/.cell
|
||||
CELL_CORE_PACKAGE = $(CELL_SHOP)/packages/core
|
||||
|
||||
# .cm sources that compile to .mach bytecode
|
||||
MACH_SOURCES = tokenize.cm parse.cm fold.cm mcode.cm \
|
||||
internal/bootstrap.cm internal/engine.cm
|
||||
|
||||
maker: install
|
||||
|
||||
makecell:
|
||||
@@ -16,7 +22,7 @@ makecell:
|
||||
cp cell /opt/homebrew/bin/
|
||||
|
||||
# Install core: symlink this directory to ~/.cell/core
|
||||
install: bootstrap $(CELL_SHOP)
|
||||
install: bootstrap .mach.stamp $(CELL_SHOP)
|
||||
@echo "Linking cell core to $(CELL_CORE_PACKAGE)"
|
||||
rm -rf $(CELL_CORE_PACKAGE)
|
||||
ln -s $(PWD) $(CELL_CORE_PACKAGE)
|
||||
@@ -40,6 +46,16 @@ libcell_runtime.dylib: $(CELL_SHOP)/build/dynamic
|
||||
cell_main: source/main.c libcell_runtime.dylib
|
||||
cc -o cell_main source/main.c -L. -lcell_runtime -Wl,-rpath,@loader_path -Wl,-rpath,/opt/homebrew/lib
|
||||
|
||||
# Regenerate .mach bytecode when any .cm source changes
|
||||
.mach.stamp: $(MACH_SOURCES)
|
||||
./cell --core . regen.cm
|
||||
@touch $@
|
||||
|
||||
# Force-regenerate all .mach bytecode files
|
||||
regen:
|
||||
./cell --core . regen.cm
|
||||
@touch .mach.stamp
|
||||
|
||||
# Create the cell shop directories
|
||||
$(CELL_SHOP):
|
||||
mkdir -p $(CELL_SHOP)
|
||||
@@ -57,8 +73,8 @@ static:
|
||||
|
||||
# Bootstrap: build cell from scratch using meson (only needed once)
|
||||
# Also installs core scripts to ~/.cell/core
|
||||
bootstrap:
|
||||
meson setup build_bootstrap -Dbuildtype=debug -Db_sanitize=address
|
||||
bootstrap:
|
||||
meson setup build_bootstrap -Dbuildtype=debugoptimized
|
||||
meson compile -C build_bootstrap
|
||||
cp build_bootstrap/cell .
|
||||
cp build_bootstrap/libcell_runtime.dylib .
|
||||
@@ -68,7 +84,7 @@ bootstrap:
|
||||
# Clean build artifacts
|
||||
clean:
|
||||
rm -rf $(CELL_SHOP)/build build_bootstrap
|
||||
rm -f cell cell_main libcell_runtime.dylib
|
||||
rm -f cell cell_main libcell_runtime.dylib .mach.stamp
|
||||
|
||||
# Ensure dynamic build directory exists
|
||||
$(CELL_SHOP)/build/dynamic: $(CELL_SHOP)
|
||||
@@ -79,4 +95,4 @@ meson:
|
||||
meson setup build_dbg -Dbuildtype=debugoptimized
|
||||
meson install -C build_dbg
|
||||
|
||||
.PHONY: cell static bootstrap clean meson install
|
||||
.PHONY: cell static bootstrap clean meson install regen
|
||||
|
||||
@@ -319,7 +319,7 @@ JSValue js_reader_list(JSContext *js, JSValue self, int argc, JSValue *argv)
|
||||
JS_FreeValue(js, arr);
|
||||
return filename;
|
||||
}
|
||||
JS_SetPropertyUint32(js, arr, arr_index++, filename);
|
||||
JS_SetPropertyNumber(js, arr, arr_index++, filename);
|
||||
}
|
||||
|
||||
return arr;
|
||||
@@ -382,13 +382,13 @@ static const JSCFunctionListEntry js_reader_funcs[] = {
|
||||
JSValue js_miniz_use(JSContext *js)
|
||||
{
|
||||
JS_NewClassID(&js_reader_class_id);
|
||||
JS_NewClass(JS_GetRuntime(js), js_reader_class_id, &js_reader_class);
|
||||
JS_NewClass(js, js_reader_class_id, &js_reader_class);
|
||||
JSValue reader_proto = JS_NewObject(js);
|
||||
JS_SetPropertyFunctionList(js, reader_proto, js_reader_funcs, sizeof(js_reader_funcs) / sizeof(JSCFunctionListEntry));
|
||||
JS_SetClassProto(js, js_reader_class_id, reader_proto);
|
||||
|
||||
JS_NewClassID(&js_writer_class_id);
|
||||
JS_NewClass(JS_GetRuntime(js), js_writer_class_id, &js_writer_class);
|
||||
JS_NewClass(js, js_writer_class_id, &js_writer_class);
|
||||
JSValue writer_proto = JS_NewObject(js);
|
||||
JS_SetPropertyFunctionList(js, writer_proto, js_writer_funcs, sizeof(js_writer_funcs) / sizeof(JSCFunctionListEntry));
|
||||
JS_SetClassProto(js, js_writer_class_id, writer_proto);
|
||||
|
||||
42
bootstrap.ce
Normal file
42
bootstrap.ce
Normal file
@@ -0,0 +1,42 @@
|
||||
// bootstrap.ce — regenerate .mach bytecode files consumed by the mach engine
|
||||
// usage: cell bootstrap.ce
|
||||
|
||||
var fd = use("fd")
|
||||
var json = use("json")
|
||||
var tokenize = use("tokenize")
|
||||
var parse = use("parse")
|
||||
var fold = use("fold")
|
||||
|
||||
var files = [
|
||||
{src: "tokenize.cm", name: "tokenize", out: "tokenize.mach"},
|
||||
{src: "parse.cm", name: "parse", out: "parse.mach"},
|
||||
{src: "fold.cm", name: "fold", out: "fold.mach"},
|
||||
{src: "mcode.cm", name: "mcode", out: "mcode.mach"},
|
||||
{src: "internal/bootstrap.cm", name: "bootstrap", out: "internal/bootstrap.mach"},
|
||||
{src: "internal/engine.cm", name: "engine", out: "internal/engine.mach"}
|
||||
]
|
||||
|
||||
var i = 0
|
||||
var entry = null
|
||||
var src = null
|
||||
var tok_result = null
|
||||
var ast = null
|
||||
var folded = null
|
||||
var ast_json = null
|
||||
var bytecode = null
|
||||
var f = null
|
||||
|
||||
while (i < length(files)) {
|
||||
entry = files[i]
|
||||
src = text(fd.slurp(entry.src))
|
||||
tok_result = tokenize(src, entry.src)
|
||||
ast = parse(tok_result.tokens, src, entry.src, tokenize)
|
||||
folded = fold(ast)
|
||||
ast_json = json.encode(folded)
|
||||
bytecode = mach_compile_ast(entry.name, ast_json)
|
||||
f = fd.open(entry.out, "w")
|
||||
fd.write(f, bytecode)
|
||||
fd.close(f)
|
||||
print(`wrote ${entry.out}`)
|
||||
i = i + 1
|
||||
}
|
||||
102
compile.ce
Normal file
102
compile.ce
Normal file
@@ -0,0 +1,102 @@
|
||||
// compile.ce — compile a .cm module to native .dylib via QBE
|
||||
//
|
||||
// Usage:
|
||||
// cell --core . compile.ce <file.cm>
|
||||
//
|
||||
// Produces <file>.dylib in the current directory.
|
||||
|
||||
var fd = use('fd')
|
||||
var os = use('os')
|
||||
|
||||
if (length(args) < 1) {
|
||||
print('usage: cell --core . compile.ce <file.cm>')
|
||||
return
|
||||
}
|
||||
|
||||
var file = args[0]
|
||||
var base = file
|
||||
if (ends_with(base, '.cm')) {
|
||||
base = text(base, 0, length(base) - 3)
|
||||
}
|
||||
|
||||
var safe = replace(replace(base, '/', '_'), '-', '_')
|
||||
var symbol = 'js_' + safe + '_use'
|
||||
var tmp = '/tmp/qbe_' + safe
|
||||
var ssa_path = tmp + '.ssa'
|
||||
var s_path = tmp + '.s'
|
||||
var o_path = tmp + '.o'
|
||||
var rt_o_path = '/tmp/qbe_rt.o'
|
||||
var dylib_path = base + '.dylib'
|
||||
var cwd = fd.getcwd()
|
||||
var rc = 0
|
||||
|
||||
// Step 1: emit QBE IL
|
||||
print('emit qbe...')
|
||||
rc = os.system('cd ' + cwd + ' && ./cell --core . --emit-qbe ' + file + ' > ' + ssa_path)
|
||||
if (rc != 0) {
|
||||
print('failed to emit qbe il')
|
||||
return
|
||||
}
|
||||
|
||||
// Step 2: post-process — insert dead labels after ret/jmp, append wrapper
|
||||
// Use awk via shell to avoid blob/slurpwrite issues with long strings
|
||||
print('post-process...')
|
||||
var awk_cmd = `awk '
|
||||
need_label && /^[[:space:]]*[^@}]/ && NF > 0 {
|
||||
print "@_dead_" dead_id; dead_id++; need_label=0
|
||||
}
|
||||
/^@/ || /^}/ || NF==0 { need_label=0 }
|
||||
/^[[:space:]]*ret / || /^[[:space:]]*jmp / { need_label=1; print; next }
|
||||
{ print }
|
||||
' ` + ssa_path + ` > ` + tmp + `_fixed.ssa`
|
||||
rc = os.system(awk_cmd)
|
||||
if (rc != 0) {
|
||||
print('post-process failed')
|
||||
return
|
||||
}
|
||||
|
||||
// Append wrapper function — called as symbol(ctx) by os.dylib_symbol.
|
||||
// Delegates to cell_rt_module_entry which heap-allocates a frame
|
||||
// (so closures survive) and calls cell_main.
|
||||
var wrapper_cmd = `printf '\nexport function l $` + symbol + `(l %%ctx) {\n@entry\n %%result =l call $cell_rt_module_entry(l %%ctx)\n ret %%result\n}\n' >> ` + tmp + `_fixed.ssa`
|
||||
rc = os.system(wrapper_cmd)
|
||||
if (rc != 0) {
|
||||
print('wrapper append failed')
|
||||
return
|
||||
}
|
||||
|
||||
// Step 3: compile QBE IL to assembly
|
||||
print('qbe compile...')
|
||||
rc = os.system('~/.local/bin/qbe -o ' + s_path + ' ' + tmp + '_fixed.ssa')
|
||||
if (rc != 0) {
|
||||
print('qbe compilation failed')
|
||||
return
|
||||
}
|
||||
|
||||
// Step 4: assemble
|
||||
print('assemble...')
|
||||
rc = os.system('cc -c ' + s_path + ' -o ' + o_path)
|
||||
if (rc != 0) {
|
||||
print('assembly failed')
|
||||
return
|
||||
}
|
||||
|
||||
// Step 5: compile runtime stubs (cached — skip if already built)
|
||||
if (!fd.is_file(rt_o_path)) {
|
||||
print('compile runtime stubs...')
|
||||
rc = os.system('cc -c ' + cwd + '/qbe_rt.c -o ' + rt_o_path + ' -fPIC')
|
||||
if (rc != 0) {
|
||||
print('runtime stubs compilation failed')
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Step 6: link dylib
|
||||
print('link...')
|
||||
rc = os.system('cc -shared -fPIC -undefined dynamic_lookup ' + o_path + ' ' + rt_o_path + ' -o ' + cwd + '/' + dylib_path)
|
||||
if (rc != 0) {
|
||||
print('linking failed')
|
||||
return
|
||||
}
|
||||
|
||||
print('built: ' + dylib_path)
|
||||
@@ -4,7 +4,7 @@
|
||||
JSC_CCALL(debug_stack_depth, return number2js(js,js_debugger_stack_depth(js)))
|
||||
|
||||
// Return a backtrace of the current call stack.
|
||||
JSC_CCALL(debug_build_backtrace, return js_debugger_build_backtrace(js,NULL))
|
||||
JSC_CCALL(debug_build_backtrace, return js_debugger_build_backtrace(js))
|
||||
|
||||
// Return the closure variables for a given function.
|
||||
JSC_CCALL(debug_closure_vars, return js_debugger_closure_variables(js,argv[0]))
|
||||
@@ -21,7 +21,7 @@ JSC_CCALL(debug_local_vars, return js_debugger_local_variables(js, js2number(js,
|
||||
JSC_CCALL(debug_fn_info, return js_debugger_fn_info(js, argv[0]))
|
||||
|
||||
// Return an array of functions in the current backtrace.
|
||||
JSC_CCALL(debug_backtrace_fns, return js_debugger_backtrace_fns(js,NULL))
|
||||
JSC_CCALL(debug_backtrace_fns, return js_debugger_backtrace_fns(js))
|
||||
|
||||
static const JSCFunctionListEntry js_debug_funcs[] = {
|
||||
MIST_FUNC_DEF(debug, stack_depth, 0),
|
||||
|
||||
49
debug/js.c
49
debug/js.c
@@ -13,7 +13,6 @@ JSC_CCALL(os_calc_mem,
|
||||
JS_SetPropertyStr(js,ret,"memory_used_size",number2js(js,mu.memory_used_size));
|
||||
JS_SetPropertyStr(js,ret,"malloc_count",number2js(js,mu.malloc_count));
|
||||
JS_SetPropertyStr(js,ret,"memory_used_count",number2js(js,mu.memory_used_count));
|
||||
/* atom_count and atom_size removed - atoms are now just strings */
|
||||
JS_SetPropertyStr(js,ret,"str_count",number2js(js,mu.str_count));
|
||||
JS_SetPropertyStr(js,ret,"str_size",number2js(js,mu.str_size));
|
||||
JS_SetPropertyStr(js,ret,"obj_count",number2js(js,mu.obj_count));
|
||||
@@ -35,47 +34,6 @@ JSC_CCALL(os_calc_mem,
|
||||
JS_SetPropertyStr(js,ret,"binary_object_size",number2js(js,mu.binary_object_size));
|
||||
)
|
||||
|
||||
// Evaluate a string of JavaScript code in the current QuickJS context.
|
||||
JSC_SSCALL(os_eval,
|
||||
if (!str2) return JS_ThrowReferenceError(js, "Second argument should be the script.");
|
||||
if (!str) return JS_ThrowReferenceError(js, "First argument should be the name of the script.");
|
||||
ret = JS_Eval(js,str2,strlen(str2),str, 0);
|
||||
)
|
||||
|
||||
// Compile a string of JavaScript code into a function object.
|
||||
JSC_SSCALL(js_compile,
|
||||
if (!str2) return JS_ThrowReferenceError(js, "Second argument should be the script.");
|
||||
if (!str) return JS_ThrowReferenceError(js, "First argument should be the name of the script.");
|
||||
ret = JS_Eval(js, str2, strlen(str2), str, JS_EVAL_FLAG_COMPILE_ONLY | JS_EVAL_FLAG_BACKTRACE_BARRIER);
|
||||
)
|
||||
|
||||
// Evaluate a function object in the current QuickJS context.
|
||||
JSC_CCALL(js_eval_compile,
|
||||
JS_DupValue(js,argv[0]);
|
||||
ret = JS_EvalFunction(js, argv[0]);
|
||||
)
|
||||
|
||||
// Compile a function object into a bytecode blob.
|
||||
JSC_CCALL(js_compile_blob,
|
||||
size_t size;
|
||||
uint8_t *data = JS_WriteObject(js, &size, argv[0], JS_WRITE_OBJ_BYTECODE);
|
||||
if (!data) {
|
||||
return JS_ThrowInternalError(js, "Failed to serialize bytecode");
|
||||
}
|
||||
ret = js_new_blob_stoned_copy(js, data, size);
|
||||
js_free(js, data);
|
||||
)
|
||||
|
||||
// Compile a bytecode blob into a function object.
|
||||
JSC_CCALL(js_compile_unblob,
|
||||
size_t size;
|
||||
void *data = js_get_blob_data(js, &size, argv[0]);
|
||||
if (data == -1) return JS_EXCEPTION;
|
||||
if (!data) return JS_ThrowReferenceError(js, "No data present in blob.");
|
||||
|
||||
return JS_ReadObject(js, data, size, JS_READ_OBJ_BYTECODE);
|
||||
)
|
||||
|
||||
// Disassemble a function object into a string.
|
||||
JSC_CCALL(js_disassemble,
|
||||
return js_debugger_fn_bytecode(js, argv[0]);
|
||||
@@ -90,11 +48,6 @@ static const JSCFunctionListEntry js_js_funcs[] = {
|
||||
MIST_FUNC_DEF(os, calc_mem, 0),
|
||||
MIST_FUNC_DEF(os, mem_limit, 1),
|
||||
MIST_FUNC_DEF(os, max_stacksize, 1),
|
||||
MIST_FUNC_DEF(os, eval, 2),
|
||||
MIST_FUNC_DEF(js, compile, 2),
|
||||
MIST_FUNC_DEF(js, eval_compile, 1),
|
||||
MIST_FUNC_DEF(js, compile_blob, 1),
|
||||
MIST_FUNC_DEF(js, compile_unblob, 1),
|
||||
MIST_FUNC_DEF(js, disassemble, 1),
|
||||
MIST_FUNC_DEF(js, fn_info, 1),
|
||||
};
|
||||
@@ -103,4 +56,4 @@ JSValue js_js_use(JSContext *js) {
|
||||
JSValue mod = JS_NewObject(js);
|
||||
JS_SetPropertyFunctionList(js,mod,js_js_funcs,countof(js_js_funcs));
|
||||
return mod;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
nav:
|
||||
- index.md
|
||||
- cellscript.md
|
||||
- actors.md
|
||||
- packages.md
|
||||
- cli.md
|
||||
- c-modules.md
|
||||
- Standard Library: library
|
||||
|
||||
90
docs/_index.md
Normal file
90
docs/_index.md
Normal file
@@ -0,0 +1,90 @@
|
||||
---
|
||||
title: "Documentation"
|
||||
description: "ƿit language documentation"
|
||||
type: "docs"
|
||||
---
|
||||
|
||||

|
||||
|
||||
ƿit is an actor-based scripting language for building concurrent applications. It combines a familiar C-like syntax with the actor model of computation, optimized for low memory usage and simplicity.
|
||||
|
||||
## Key Features
|
||||
|
||||
- **Actor Model** — isolated memory, message passing, no shared state
|
||||
- **Immutability** — `stone()` makes values permanently frozen
|
||||
- **Prototype Inheritance** — objects without classes
|
||||
- **C Integration** — seamlessly extend with native code
|
||||
- **Cross-Platform** — deploy to desktop, web, and embedded
|
||||
|
||||
## Quick Start
|
||||
|
||||
```javascript
|
||||
// hello.ce - A simple actor
|
||||
print("Hello, ƿit!")
|
||||
$stop()
|
||||
```
|
||||
|
||||
```bash
|
||||
pit hello
|
||||
```
|
||||
|
||||
## Language
|
||||
|
||||
- [**ƿit Language**](/docs/language/) — syntax, types, and operators
|
||||
- [**Actors and Modules**](/docs/actors/) — the execution model
|
||||
- [**Requestors**](/docs/requestors/) — asynchronous composition
|
||||
- [**Packages**](/docs/packages/) — code organization and sharing
|
||||
|
||||
## Reference
|
||||
|
||||
- [**Built-in Functions**](/docs/functions/) — intrinsics reference
|
||||
- [text](/docs/library/text/) — text conversion and manipulation
|
||||
- [number](/docs/library/number/) — numeric conversion and operations
|
||||
- [array](/docs/library/array/) — array creation and manipulation
|
||||
- [object](/docs/library/object/) — object creation, prototypes, and serialization
|
||||
|
||||
## Standard Library
|
||||
|
||||
Modules loaded with `use()`:
|
||||
|
||||
- [blob](/docs/library/blob/) — binary data
|
||||
- [time](/docs/library/time/) — time and dates
|
||||
- [math](/docs/library/math/) — trigonometry and math
|
||||
- [json](/docs/library/json/) — JSON encoding/decoding
|
||||
- [random](/docs/library/random/) — random numbers
|
||||
|
||||
## Tools
|
||||
|
||||
- [**Command Line**](/docs/cli/) — the `pit` tool
|
||||
- [**Writing C Modules**](/docs/c-modules/) — native extensions
|
||||
|
||||
## Architecture
|
||||
|
||||
ƿit programs are organized into **packages**. Each package contains:
|
||||
|
||||
- **Modules** (`.cm`) — return a value, cached and frozen
|
||||
- **Actors** (`.ce`) — run independently, communicate via messages
|
||||
- **C files** (`.c`) — compiled to native libraries
|
||||
|
||||
Actors never share memory. They communicate by sending messages, which are automatically serialized. This makes concurrent programming safe and predictable.
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
# Clone and bootstrap
|
||||
git clone https://gitea.pockle.world/john/cell
|
||||
cd cell
|
||||
make bootstrap
|
||||
```
|
||||
|
||||
The ƿit shop is stored at `~/.pit/`.
|
||||
|
||||
## Development
|
||||
|
||||
After making changes, recompile with:
|
||||
|
||||
```bash
|
||||
make
|
||||
```
|
||||
|
||||
Run `cell --help` to see all available CLI flags.
|
||||
@@ -1,10 +1,15 @@
|
||||
# Actors and Modules
|
||||
---
|
||||
title: "Actors and Modules"
|
||||
description: "The ƿit execution model"
|
||||
weight: 20
|
||||
type: "docs"
|
||||
---
|
||||
|
||||
Cell organizes code into two types of scripts: **modules** (`.cm`) and **actors** (`.ce`).
|
||||
ƿit organizes code into two types of scripts: **modules** (`.cm`) and **actors** (`.ce`).
|
||||
|
||||
## The Actor Model
|
||||
|
||||
Cell is built on the actor model of computation. Each actor:
|
||||
ƿit is built on the actor model of computation. Each actor:
|
||||
|
||||
- Has its own **isolated memory** — actors never share state
|
||||
- Runs to completion each **turn** — no preemption
|
||||
@@ -21,13 +26,13 @@ A module is a script that **returns a value**. The returned value is cached and
|
||||
// math_utils.cm
|
||||
var math = use('math/radians')
|
||||
|
||||
function distance(x1, y1, x2, y2) {
|
||||
var distance = function(x1, y1, x2, y2) {
|
||||
var dx = x2 - x1
|
||||
var dy = y2 - y1
|
||||
return math.sqrt(dx * dx + dy * dy)
|
||||
}
|
||||
|
||||
function midpoint(x1, y1, x2, y2) {
|
||||
var midpoint = function(x1, y1, x2, y2) {
|
||||
return {
|
||||
x: (x1 + x2) / 2,
|
||||
y: (y1 + y2) / 2
|
||||
@@ -60,12 +65,12 @@ An actor is a script that **does not return a value**. It runs as an independent
|
||||
|
||||
```javascript
|
||||
// worker.ce
|
||||
log.console("Worker started")
|
||||
print("Worker started")
|
||||
|
||||
$on_message = function(msg) {
|
||||
log.console("Received:", msg)
|
||||
$receiver(function(msg, reply) {
|
||||
print("Received:", msg)
|
||||
// Process message...
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
**Key properties:**
|
||||
@@ -83,7 +88,7 @@ Actors have access to special functions prefixed with `$`:
|
||||
Reference to the current actor.
|
||||
|
||||
```javascript
|
||||
log.console($me) // actor reference
|
||||
print($me) // actor reference
|
||||
```
|
||||
|
||||
### $stop()
|
||||
@@ -100,7 +105,7 @@ Send a message to another actor.
|
||||
|
||||
```javascript
|
||||
$send(other_actor, {type: "ping", data: 42}, function(reply) {
|
||||
log.console("Got reply:", reply)
|
||||
print("Got reply:", reply)
|
||||
})
|
||||
```
|
||||
|
||||
@@ -112,7 +117,7 @@ Start a new actor from a script.
|
||||
|
||||
```javascript
|
||||
$start(function(new_actor) {
|
||||
log.console("Started:", new_actor)
|
||||
print("Started:", new_actor)
|
||||
}, "worker")
|
||||
```
|
||||
|
||||
@@ -122,7 +127,7 @@ Schedule a callback after a delay.
|
||||
|
||||
```javascript
|
||||
$delay(function() {
|
||||
log.console("5 seconds later")
|
||||
print("5 seconds later")
|
||||
}, 5)
|
||||
```
|
||||
|
||||
@@ -169,19 +174,47 @@ $contact(function(connection) {
|
||||
|
||||
### $time_limit(requestor, seconds)
|
||||
|
||||
Wrap a requestor with a timeout.
|
||||
Wrap a requestor with a timeout. See [Requestors](/docs/requestors/) for details.
|
||||
|
||||
```javascript
|
||||
$time_limit(my_requestor, 10) // 10 second timeout
|
||||
```
|
||||
|
||||
### $couple(actor)
|
||||
|
||||
Couple the current actor to another actor. When the coupled actor dies, the current actor also dies. Coupling is automatic between an actor and its overling (parent).
|
||||
|
||||
```javascript
|
||||
$couple(other_actor)
|
||||
```
|
||||
|
||||
### $unneeded(callback, seconds)
|
||||
|
||||
Schedule the actor for removal after a specified time.
|
||||
|
||||
```javascript
|
||||
$unneeded(function() {
|
||||
// cleanup before removal
|
||||
}, 30)
|
||||
```
|
||||
|
||||
### $connection(callback, actor, config)
|
||||
|
||||
Get information about the connection to another actor, such as latency, bandwidth, and activity.
|
||||
|
||||
```javascript
|
||||
$connection(function(info) {
|
||||
print(info.latency)
|
||||
}, other_actor, {})
|
||||
```
|
||||
|
||||
## Module Resolution
|
||||
|
||||
When you call `use('name')`, Cell searches:
|
||||
When you call `use('name')`, ƿit searches:
|
||||
|
||||
1. **Current package** — files relative to package root
|
||||
2. **Dependencies** — packages declared in `cell.toml`
|
||||
3. **Core** — built-in Cell modules
|
||||
2. **Dependencies** — packages declared in `pit.toml`
|
||||
3. **Core** — built-in ƿit modules
|
||||
|
||||
```javascript
|
||||
// From within package 'myapp':
|
||||
@@ -199,14 +232,14 @@ Files starting with underscore (`_helper.cm`) are private to the package.
|
||||
// main.ce - Entry point
|
||||
var config = use('config')
|
||||
|
||||
log.console("Starting application...")
|
||||
print("Starting application...")
|
||||
|
||||
$start(function(worker) {
|
||||
$send(worker, {task: "process", data: [1, 2, 3]})
|
||||
}, "worker")
|
||||
|
||||
$delay(function() {
|
||||
log.console("Shutting down")
|
||||
print("Shutting down")
|
||||
$stop()
|
||||
}, 10)
|
||||
```
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
# Writing C Modules
|
||||
---
|
||||
title: "Writing C Modules"
|
||||
description: "Extending ƿit with native code"
|
||||
weight: 50
|
||||
type: "docs"
|
||||
---
|
||||
|
||||
Cell makes it easy to extend functionality with C code. C files in a package are compiled into a dynamic library and can be imported like any other module.
|
||||
ƿit makes it easy to extend functionality with C code. C files in a package are compiled into a dynamic library and can be imported like any other module.
|
||||
|
||||
## Basic Structure
|
||||
|
||||
@@ -45,12 +50,12 @@ Where:
|
||||
- `<filename>` is the C file name without extension
|
||||
|
||||
Examples:
|
||||
- `mypackage/math.c` → `js_mypackage_math_use`
|
||||
- `gitea.pockle.world/john/lib/render.c` → `js_gitea_pockle_world_john_lib_render_use`
|
||||
- `mypackage/math.c` -> `js_mypackage_math_use`
|
||||
- `gitea.pockle.world/john/lib/render.c` -> `js_gitea_pockle_world_john_lib_render_use`
|
||||
|
||||
## Required Headers
|
||||
|
||||
Include `cell.h` for all Cell integration:
|
||||
Include `cell.h` for all ƿit integration:
|
||||
|
||||
```c
|
||||
#include "cell.h"
|
||||
@@ -63,7 +68,7 @@ This provides:
|
||||
|
||||
## Conversion Functions
|
||||
|
||||
### JavaScript ↔ C
|
||||
### JavaScript <-> C
|
||||
|
||||
```c
|
||||
// Numbers
|
||||
@@ -201,7 +206,7 @@ static const JSCFunctionListEntry js_funcs[] = {
|
||||
CELL_USE_FUNCS(js_funcs)
|
||||
```
|
||||
|
||||
Usage in Cell:
|
||||
Usage in ƿit:
|
||||
|
||||
```javascript
|
||||
var vector = use('vector')
|
||||
@@ -211,7 +216,7 @@ var n = vector.normalize(3, 4) // {x: 0.6, y: 0.8}
|
||||
var d = vector.dot(1, 0, 0, 1) // 0
|
||||
```
|
||||
|
||||
## Combining C and Cell
|
||||
## Combining C and ƿit
|
||||
|
||||
A common pattern is to have a C file provide low-level functions and a `.cm` file provide a higher-level API:
|
||||
|
||||
@@ -224,7 +229,7 @@ A common pattern is to have a C file provide low-level functions and a `.cm` fil
|
||||
// vector.cm
|
||||
var native = this // C module passed as 'this'
|
||||
|
||||
function Vector(x, y) {
|
||||
var Vector = function(x, y) {
|
||||
return {x: x, y: y}
|
||||
}
|
||||
|
||||
@@ -244,11 +249,11 @@ return Vector
|
||||
C files are automatically compiled when you run:
|
||||
|
||||
```bash
|
||||
cell build
|
||||
cell update
|
||||
pit build
|
||||
pit update
|
||||
```
|
||||
|
||||
The resulting dynamic library is placed in `~/.cell/lib/`.
|
||||
The resulting dynamic library is placed in `~/.pit/lib/`.
|
||||
|
||||
## Platform-Specific Code
|
||||
|
||||
@@ -260,7 +265,7 @@ audio_playdate.c # Playdate
|
||||
audio_emscripten.c # Web/Emscripten
|
||||
```
|
||||
|
||||
Cell selects the appropriate file based on the target platform.
|
||||
ƿit selects the appropriate file based on the target platform.
|
||||
|
||||
## Static Declarations
|
||||
|
||||
|
||||
@@ -1,288 +0,0 @@
|
||||
# Cell Language
|
||||
|
||||
Cell is a scripting language for actor-based programming. It combines a familiar syntax with a prototype-based object system and strict immutability semantics.
|
||||
|
||||
## Basics
|
||||
|
||||
### Variables and Constants
|
||||
|
||||
```javascript
|
||||
var x = 10 // mutable variable (block-scoped like let)
|
||||
def PI = 3.14159 // constant (cannot be reassigned)
|
||||
```
|
||||
|
||||
### Data Types
|
||||
|
||||
Cell has six fundamental types:
|
||||
|
||||
- **number** — DEC64 decimal floating point (no rounding errors)
|
||||
- **text** — Unicode strings
|
||||
- **logical** — `true` or `false`
|
||||
- **null** — the absence of a value (no `undefined`)
|
||||
- **array** — ordered, numerically-indexed sequences
|
||||
- **object** — key-value records with prototype inheritance
|
||||
- **blob** — binary data (bits, not bytes)
|
||||
- **function** — first-class callable values
|
||||
|
||||
### Literals
|
||||
|
||||
```javascript
|
||||
// Numbers
|
||||
42
|
||||
3.14
|
||||
1_000_000 // underscores for readability
|
||||
|
||||
// Text
|
||||
"hello"
|
||||
'world'
|
||||
`template ${x}` // string interpolation
|
||||
|
||||
// Logical
|
||||
true
|
||||
false
|
||||
|
||||
// Null
|
||||
null
|
||||
|
||||
// Arrays
|
||||
[1, 2, 3]
|
||||
["a", "b", "c"]
|
||||
|
||||
// Objects
|
||||
{name: "cell", version: 1}
|
||||
{x: 10, y: 20}
|
||||
```
|
||||
|
||||
### Operators
|
||||
|
||||
```javascript
|
||||
// Arithmetic
|
||||
+ - * / %
|
||||
** // exponentiation
|
||||
|
||||
// Comparison (always strict)
|
||||
== // equals (like === in JS)
|
||||
!= // not equals (like !== in JS)
|
||||
< > <= >=
|
||||
|
||||
// Logical
|
||||
&& || !
|
||||
|
||||
// Assignment
|
||||
= += -= *= /=
|
||||
```
|
||||
|
||||
### Control Flow
|
||||
|
||||
```javascript
|
||||
// Conditionals
|
||||
if (x > 0) {
|
||||
log.console("positive")
|
||||
} else if (x < 0) {
|
||||
log.console("negative")
|
||||
} else {
|
||||
log.console("zero")
|
||||
}
|
||||
|
||||
// Ternary
|
||||
var sign = x > 0 ? 1 : -1
|
||||
|
||||
// Loops
|
||||
for (var i = 0; i < 10; i++) {
|
||||
log.console(i)
|
||||
}
|
||||
|
||||
for (var item of items) {
|
||||
log.console(item)
|
||||
}
|
||||
|
||||
for (var key in obj) {
|
||||
log.console(key, obj[key])
|
||||
}
|
||||
|
||||
while (condition) {
|
||||
// body
|
||||
}
|
||||
|
||||
// Control
|
||||
break
|
||||
continue
|
||||
return value
|
||||
throw "error message"
|
||||
```
|
||||
|
||||
### Functions
|
||||
|
||||
```javascript
|
||||
// Named function
|
||||
function add(a, b) {
|
||||
return a + b
|
||||
}
|
||||
|
||||
// Anonymous function
|
||||
var multiply = function(a, b) {
|
||||
return a * b
|
||||
}
|
||||
|
||||
// Arrow function
|
||||
var square = x => x * x
|
||||
var sum = (a, b) => a + b
|
||||
|
||||
// Rest parameters
|
||||
function log_all(...args) {
|
||||
for (var arg of args) log.console(arg)
|
||||
}
|
||||
|
||||
// Default parameters
|
||||
function greet(name, greeting = "Hello") {
|
||||
return `${greeting}, ${name}!`
|
||||
}
|
||||
```
|
||||
|
||||
All closures capture `this` (like arrow functions in JavaScript).
|
||||
|
||||
## Arrays
|
||||
|
||||
Arrays are **distinct from objects**. They are ordered, numerically-indexed sequences. You cannot add arbitrary string keys to an array.
|
||||
|
||||
```javascript
|
||||
var arr = [1, 2, 3]
|
||||
arr[0] // 1
|
||||
arr[2] = 10 // [1, 2, 10]
|
||||
length(arr) // 3
|
||||
|
||||
// Array spread
|
||||
var more = [...arr, 4, 5] // [1, 2, 10, 4, 5]
|
||||
```
|
||||
|
||||
## Objects
|
||||
|
||||
Objects are key-value records with prototype-based inheritance.
|
||||
|
||||
```javascript
|
||||
var point = {x: 10, y: 20}
|
||||
point.x // 10
|
||||
point["y"] // 20
|
||||
|
||||
// Object spread
|
||||
var point3d = {...point, z: 30}
|
||||
|
||||
// Prototype inheritance
|
||||
var colored_point = {__proto__: point, color: "red"}
|
||||
colored_point.x // 10 (inherited)
|
||||
```
|
||||
|
||||
### Prototypes
|
||||
|
||||
```javascript
|
||||
// Create object with prototype
|
||||
var child = meme(parent)
|
||||
|
||||
// Get prototype
|
||||
var p = proto(child)
|
||||
|
||||
// Check prototype chain
|
||||
isa(child, parent) // true
|
||||
```
|
||||
|
||||
## Immutability with Stone
|
||||
|
||||
The `stone()` function makes values permanently immutable.
|
||||
|
||||
```javascript
|
||||
var config = stone({
|
||||
debug: true,
|
||||
maxRetries: 3
|
||||
})
|
||||
|
||||
config.debug = false // Error! Stone objects cannot be modified
|
||||
```
|
||||
|
||||
Stone is **deep** — all nested objects and arrays are also frozen. This cannot be reversed.
|
||||
|
||||
```javascript
|
||||
stone.p(value) // returns true if value is stone
|
||||
```
|
||||
|
||||
## Built-in Functions
|
||||
|
||||
### length(value)
|
||||
|
||||
Returns the length of arrays (elements), text (codepoints), blobs (bits), or functions (arity).
|
||||
|
||||
```javascript
|
||||
length([1, 2, 3]) // 3
|
||||
length("hello") // 5
|
||||
length(function(a,b){}) // 2
|
||||
```
|
||||
|
||||
### use(path)
|
||||
|
||||
Import a module. Returns the cached, stone value.
|
||||
|
||||
```javascript
|
||||
var math = use('math/radians')
|
||||
var json = use('json')
|
||||
```
|
||||
|
||||
### isa(value, type)
|
||||
|
||||
Check type or prototype chain.
|
||||
|
||||
```javascript
|
||||
is_number(42) // true
|
||||
is_text("hi") // true
|
||||
is_array([1,2]) // true
|
||||
is_object({}) // true
|
||||
isa(child, parent) // true if parent is in prototype chain
|
||||
```
|
||||
|
||||
### reverse(array)
|
||||
|
||||
Returns a new array with elements in reverse order.
|
||||
|
||||
```javascript
|
||||
reverse([1, 2, 3]) // [3, 2, 1]
|
||||
```
|
||||
|
||||
### logical(value)
|
||||
|
||||
Convert to boolean.
|
||||
|
||||
```javascript
|
||||
logical(0) // false
|
||||
logical(1) // true
|
||||
logical("true") // true
|
||||
logical("false") // false
|
||||
logical(null) // false
|
||||
```
|
||||
|
||||
## Logging
|
||||
|
||||
```javascript
|
||||
log.console("message") // standard output
|
||||
log.error("problem") // error output
|
||||
```
|
||||
|
||||
## Pattern Matching
|
||||
|
||||
Cell supports regex patterns in string functions, but not standalone regex objects.
|
||||
|
||||
```javascript
|
||||
text.search("hello world", /world/)
|
||||
replace("hello", /l/g, "L")
|
||||
```
|
||||
|
||||
## Error Handling
|
||||
|
||||
```javascript
|
||||
try {
|
||||
riskyOperation()
|
||||
} catch (e) {
|
||||
log.error(e)
|
||||
}
|
||||
|
||||
throw "something went wrong"
|
||||
```
|
||||
|
||||
If an actor has an uncaught error, it crashes.
|
||||
105
docs/cli.md
105
docs/cli.md
@@ -1,138 +1,143 @@
|
||||
# Command Line Interface
|
||||
---
|
||||
title: "Command Line Interface"
|
||||
description: "The pit tool"
|
||||
weight: 40
|
||||
type: "docs"
|
||||
---
|
||||
|
||||
Cell provides a command-line interface for managing packages, running scripts, and building applications.
|
||||
ƿit provides a command-line interface for managing packages, running scripts, and building applications.
|
||||
|
||||
## Basic Usage
|
||||
|
||||
```bash
|
||||
cell <command> [arguments]
|
||||
pit <command> [arguments]
|
||||
```
|
||||
|
||||
## Commands
|
||||
|
||||
### cell version
|
||||
### pit version
|
||||
|
||||
Display the Cell version.
|
||||
Display the ƿit version.
|
||||
|
||||
```bash
|
||||
cell version
|
||||
pit version
|
||||
# 0.1.0
|
||||
```
|
||||
|
||||
### cell install
|
||||
### pit install
|
||||
|
||||
Install a package to the shop.
|
||||
|
||||
```bash
|
||||
cell install gitea.pockle.world/john/prosperon
|
||||
cell install /Users/john/local/mypackage # local path
|
||||
pit install gitea.pockle.world/john/prosperon
|
||||
pit install /Users/john/local/mypackage # local path
|
||||
```
|
||||
|
||||
### cell update
|
||||
### pit update
|
||||
|
||||
Update packages from remote sources.
|
||||
|
||||
```bash
|
||||
cell update # update all packages
|
||||
cell update <package> # update specific package
|
||||
pit update # update all packages
|
||||
pit update <package> # update specific package
|
||||
```
|
||||
|
||||
### cell remove
|
||||
### pit remove
|
||||
|
||||
Remove a package from the shop.
|
||||
|
||||
```bash
|
||||
cell remove gitea.pockle.world/john/oldpackage
|
||||
pit remove gitea.pockle.world/john/oldpackage
|
||||
```
|
||||
|
||||
### cell list
|
||||
### pit list
|
||||
|
||||
List installed packages.
|
||||
|
||||
```bash
|
||||
cell list # list all installed packages
|
||||
cell list <package> # list dependencies of a package
|
||||
pit list # list all installed packages
|
||||
pit list <package> # list dependencies of a package
|
||||
```
|
||||
|
||||
### cell ls
|
||||
### pit ls
|
||||
|
||||
List modules and actors in a package.
|
||||
|
||||
```bash
|
||||
cell ls # list files in current project
|
||||
cell ls <package> # list files in specified package
|
||||
pit ls # list files in current project
|
||||
pit ls <package> # list files in specified package
|
||||
```
|
||||
|
||||
### cell build
|
||||
### pit build
|
||||
|
||||
Build the current package.
|
||||
|
||||
```bash
|
||||
cell build
|
||||
pit build
|
||||
```
|
||||
|
||||
### cell test
|
||||
### pit test
|
||||
|
||||
Run tests.
|
||||
|
||||
```bash
|
||||
cell test # run tests in current package
|
||||
cell test all # run all tests
|
||||
cell test <package> # run tests in specific package
|
||||
pit test # run tests in current package
|
||||
pit test all # run all tests
|
||||
pit test <package> # run tests in specific package
|
||||
```
|
||||
|
||||
### cell link
|
||||
### pit link
|
||||
|
||||
Manage local package links for development.
|
||||
|
||||
```bash
|
||||
cell link add <canonical> <local_path> # link a package
|
||||
cell link list # show all links
|
||||
cell link delete <canonical> # remove a link
|
||||
cell link clear # remove all links
|
||||
pit link add <canonical> <local_path> # link a package
|
||||
pit link list # show all links
|
||||
pit link delete <canonical> # remove a link
|
||||
pit link clear # remove all links
|
||||
```
|
||||
|
||||
### cell fetch
|
||||
### pit fetch
|
||||
|
||||
Fetch package sources without extracting.
|
||||
|
||||
```bash
|
||||
cell fetch <package>
|
||||
pit fetch <package>
|
||||
```
|
||||
|
||||
### cell upgrade
|
||||
### pit upgrade
|
||||
|
||||
Upgrade the Cell installation itself.
|
||||
Upgrade the ƿit installation itself.
|
||||
|
||||
```bash
|
||||
cell upgrade
|
||||
pit upgrade
|
||||
```
|
||||
|
||||
### cell clean
|
||||
### pit clean
|
||||
|
||||
Clean build artifacts.
|
||||
|
||||
```bash
|
||||
cell clean
|
||||
pit clean
|
||||
```
|
||||
|
||||
### cell help
|
||||
### pit help
|
||||
|
||||
Display help information.
|
||||
|
||||
```bash
|
||||
cell help
|
||||
cell help <command>
|
||||
pit help
|
||||
pit help <command>
|
||||
```
|
||||
|
||||
## Running Scripts
|
||||
|
||||
Any `.ce` file in the Cell core can be run as a command:
|
||||
Any `.ce` file in the ƿit core can be run as a command:
|
||||
|
||||
```bash
|
||||
cell version # runs version.ce
|
||||
cell build # runs build.ce
|
||||
cell test # runs test.ce
|
||||
pit version # runs version.ce
|
||||
pit build # runs build.ce
|
||||
pit test # runs test.ce
|
||||
```
|
||||
|
||||
## Package Locators
|
||||
@@ -143,16 +148,16 @@ Packages are identified by locators:
|
||||
- **Local**: `/absolute/path/to/package`
|
||||
|
||||
```bash
|
||||
cell install gitea.pockle.world/john/prosperon
|
||||
cell install /Users/john/work/mylib
|
||||
pit install gitea.pockle.world/john/prosperon
|
||||
pit install /Users/john/work/mylib
|
||||
```
|
||||
|
||||
## Configuration
|
||||
|
||||
Cell stores its data in `~/.cell/`:
|
||||
ƿit stores its data in `~/.pit/`:
|
||||
|
||||
```
|
||||
~/.cell/
|
||||
~/.pit/
|
||||
├── packages/ # installed packages
|
||||
├── lib/ # compiled dynamic libraries
|
||||
├── build/ # build cache
|
||||
@@ -163,7 +168,7 @@ Cell stores its data in `~/.cell/`:
|
||||
|
||||
## Environment
|
||||
|
||||
Cell reads the `HOME` environment variable to locate the shop directory.
|
||||
ƿit reads the `HOME` environment variable to locate the shop directory.
|
||||
|
||||
## Exit Codes
|
||||
|
||||
|
||||
1068
docs/functions.md
1068
docs/functions.md
File diff suppressed because it is too large
Load Diff
@@ -1,66 +0,0 @@
|
||||
# Cell
|
||||
|
||||

|
||||
|
||||
Cell is an actor-based scripting language for building concurrent applications. It combines a familiar C-like syntax with the actor model of computation, optimized for low memory usage and simplicity.
|
||||
|
||||
## Key Features
|
||||
|
||||
- **Actor Model** — isolated memory, message passing, no shared state
|
||||
- **Immutability** — `stone()` makes values permanently frozen
|
||||
- **Prototype Inheritance** — objects without classes
|
||||
- **C Integration** — seamlessly extend with native code
|
||||
- **Cross-Platform** — deploy to desktop, web, and embedded
|
||||
|
||||
## Quick Start
|
||||
|
||||
```javascript
|
||||
// hello.ce - A simple actor
|
||||
log.console("Hello, Cell!")
|
||||
$stop()
|
||||
```
|
||||
|
||||
```bash
|
||||
cell hello
|
||||
```
|
||||
|
||||
## Documentation
|
||||
|
||||
- [**Cell Language**](cellscript.md) — syntax, types, and built-in functions
|
||||
- [**Actors and Modules**](actors.md) — the execution model
|
||||
- [**Packages**](packages.md) — code organization and sharing
|
||||
- [**Command Line**](cli.md) — the `cell` tool
|
||||
- [**Writing C Modules**](c-modules.md) — native extensions
|
||||
|
||||
## Standard Library
|
||||
|
||||
- [text](library/text.md) — string manipulation
|
||||
- [number](library/number.md) — numeric operations (functions are global: `floor()`, `max()`, etc.)
|
||||
- [array](library/array.md) — array utilities
|
||||
- [object](library/object.md) — object utilities
|
||||
- [blob](library/blob.md) — binary data
|
||||
- [time](library/time.md) — time and dates
|
||||
- [math](library/math.md) — trigonometry and math
|
||||
- [json](library/json.md) — JSON encoding/decoding
|
||||
- [random](library/random.md) — random numbers
|
||||
|
||||
## Architecture
|
||||
|
||||
Cell programs are organized into **packages**. Each package contains:
|
||||
|
||||
- **Modules** (`.cm`) — return a value, cached and frozen
|
||||
- **Actors** (`.ce`) — run independently, communicate via messages
|
||||
- **C files** (`.c`) — compiled to native libraries
|
||||
|
||||
Actors never share memory. They communicate by sending messages, which are automatically serialized. This makes concurrent programming safe and predictable.
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
# Clone and bootstrap
|
||||
git clone https://gitea.pockle.world/john/cell
|
||||
cd cell
|
||||
make bootstrap
|
||||
```
|
||||
|
||||
The Cell shop is stored at `~/.cell/`.
|
||||
94
docs/kim.md
Normal file
94
docs/kim.md
Normal file
@@ -0,0 +1,94 @@
|
||||
---
|
||||
title: "Kim Encoding"
|
||||
description: "Compact character and count encoding"
|
||||
weight: 80
|
||||
type: "docs"
|
||||
---
|
||||
|
||||
Kim is a character and count encoding designed by Douglas Crockford. It encodes Unicode characters and variable-length integers using continuation bytes. Kim is simpler and more compact than UTF-8 for most text.
|
||||
|
||||
## Continuation Bytes
|
||||
|
||||
The fundamental idea in Kim is the continuation byte:
|
||||
|
||||
```
|
||||
C D D D D D D D
|
||||
```
|
||||
|
||||
- **C** — continue bit. If 1, read another byte. If 0, this is the last byte.
|
||||
- **D** (7 bits) — data bits.
|
||||
|
||||
To decode: shift the accumulator left by 7 bits, add the 7 data bits. If the continue bit is 1, repeat with the next byte. If 0, the value is complete.
|
||||
|
||||
To encode: take the value, emit 7 bits at a time from most significant to least significant, setting the continue bit on all bytes except the last.
|
||||
|
||||
## Character Encoding
|
||||
|
||||
Kim encodes Unicode codepoints directly as continuation byte sequences:
|
||||
|
||||
| Range | Bytes | Characters |
|
||||
|-------|-------|------------|
|
||||
| U+0000 to U+007F | 1 | ASCII |
|
||||
| U+0080 to U+3FFF | 2 | First quarter of BMP |
|
||||
| U+4000 to U+10FFFF | 3 | All other Unicode |
|
||||
|
||||
Unlike UTF-8, there is no need for surrogate pairs or escapement. Every Unicode character, including emoji and characters from extended planes, is encoded in at most 3 bytes.
|
||||
|
||||
### Examples
|
||||
|
||||
```
|
||||
'A' (U+0041) → 41
|
||||
'é' (U+00E9) → 81 69
|
||||
'💩' (U+1F4A9) → 87 E9 29
|
||||
```
|
||||
|
||||
## Count Encoding
|
||||
|
||||
Kim is also used for encoding counts (lengths, sizes). The same continuation byte format represents non-negative integers of arbitrary size:
|
||||
|
||||
| Range | Bytes |
|
||||
|-------|-------|
|
||||
| 0 to 127 | 1 |
|
||||
| 128 to 16383 | 2 |
|
||||
| 16384 to 2097151 | 3 |
|
||||
|
||||
## Comparison with UTF-8
|
||||
|
||||
| Property | Kim | UTF-8 |
|
||||
|----------|-----|-------|
|
||||
| ASCII | 1 byte | 1 byte |
|
||||
| BMP (first quarter) | 2 bytes | 2-3 bytes |
|
||||
| Full Unicode | 3 bytes | 3-4 bytes |
|
||||
| Self-synchronizing | No | Yes |
|
||||
| Sortable | No | Yes |
|
||||
| Simpler to implement | Yes | No |
|
||||
| Byte count for counts | Variable (7 bits/byte) | Not applicable |
|
||||
|
||||
Kim trades self-synchronization (the ability to find character boundaries from any position) for simplicity and compactness. In practice, Kim text is accessed sequentially, so self-synchronization is not needed.
|
||||
|
||||
## Usage in ƿit
|
||||
|
||||
Kim is used internally by blobs and by the Nota message format.
|
||||
|
||||
### In Blobs
|
||||
|
||||
The `blob.write_text` and `blob.read_text` functions use Kim to encode text into binary data:
|
||||
|
||||
```javascript
|
||||
var blob = use('blob')
|
||||
var b = blob.make()
|
||||
blob.write_text(b, "hello") // Kim-encoded length + characters
|
||||
stone(b)
|
||||
var text = blob.read_text(b, 0) // "hello"
|
||||
```
|
||||
|
||||
### In Nota
|
||||
|
||||
Nota uses Kim for two purposes:
|
||||
|
||||
1. **Counts** — array lengths, text lengths, blob sizes, record pair counts
|
||||
2. **Characters** — text content within Nota messages
|
||||
|
||||
The preamble byte of each Nota value incorporates the first few bits of a Kim-encoded count, with the continue bit indicating whether more bytes follow.
|
||||
|
||||
See [Nota Format](#nota) for the full specification.
|
||||
649
docs/language.md
Normal file
649
docs/language.md
Normal file
@@ -0,0 +1,649 @@
|
||||
---
|
||||
title: "ƿit Language"
|
||||
description: "Syntax, types, operators, and built-in functions"
|
||||
weight: 10
|
||||
type: "docs"
|
||||
---
|
||||
|
||||
ƿit is a scripting language for actor-based programming. It combines a familiar syntax with a prototype-based object system and strict immutability semantics.
|
||||
|
||||
## Basics
|
||||
|
||||
### Variables and Constants
|
||||
|
||||
Variables are declared with `var`, constants with `def`. All declarations must be initialized and must appear at the function body level — not inside `if`, `while`, `for`, or `do` blocks.
|
||||
|
||||
```javascript
|
||||
var x = 10
|
||||
var name = "pit"
|
||||
var empty = null
|
||||
|
||||
def PI = 3.14159 // constant, cannot be reassigned
|
||||
|
||||
var a = 1, b = 2, c = 3 // multiple declarations
|
||||
```
|
||||
|
||||
### Data Types
|
||||
|
||||
ƿit has eight fundamental types:
|
||||
|
||||
- **number** — DEC64 decimal floating point (no rounding errors)
|
||||
- **text** — Unicode strings
|
||||
- **logical** — `true` or `false`
|
||||
- **null** — the absence of a value (no `undefined`)
|
||||
- **array** — ordered, numerically-indexed sequences
|
||||
- **object** — key-value records with prototype inheritance
|
||||
- **blob** — binary data (bits, not bytes)
|
||||
- **function** — first-class callable values
|
||||
|
||||
### Literals
|
||||
|
||||
```javascript
|
||||
// Numbers
|
||||
42
|
||||
3.14
|
||||
-5
|
||||
0
|
||||
1e3 // scientific notation (1000)
|
||||
|
||||
// Text
|
||||
"hello"
|
||||
`template ${x}` // string interpolation
|
||||
`${1 + 2}` // expression interpolation
|
||||
|
||||
// Logical
|
||||
true
|
||||
false
|
||||
|
||||
// Null
|
||||
null
|
||||
|
||||
// Arrays
|
||||
[1, 2, 3]
|
||||
[]
|
||||
|
||||
// Objects
|
||||
{a: 1, b: "two"}
|
||||
{}
|
||||
|
||||
// Regex
|
||||
/\d+/
|
||||
/hello/i // with flags
|
||||
```
|
||||
|
||||
## Operators
|
||||
|
||||
### Arithmetic
|
||||
|
||||
```javascript
|
||||
2 + 3 // 5
|
||||
5 - 3 // 2
|
||||
3 * 4 // 12
|
||||
12 / 4 // 3
|
||||
10 % 3 // 1
|
||||
2 ** 3 // 8 (exponentiation)
|
||||
```
|
||||
|
||||
### Comparison
|
||||
|
||||
All comparisons are strict — there is no type coercion.
|
||||
|
||||
```javascript
|
||||
5 == 5 // true
|
||||
5 != 6 // true
|
||||
3 < 5 // true
|
||||
5 > 3 // true
|
||||
3 <= 3 // true
|
||||
5 >= 5 // true
|
||||
```
|
||||
|
||||
### Logical
|
||||
|
||||
```javascript
|
||||
true && true // true
|
||||
true && false // false
|
||||
false || true // true
|
||||
false || false // false
|
||||
!true // false
|
||||
!false // true
|
||||
```
|
||||
|
||||
Logical operators short-circuit:
|
||||
|
||||
```javascript
|
||||
var called = false
|
||||
var fn = function() { called = true; return true }
|
||||
var r = false && fn() // fn() not called
|
||||
r = true || fn() // fn() not called
|
||||
```
|
||||
|
||||
### Bitwise
|
||||
|
||||
```javascript
|
||||
5 & 3 // 1 (AND)
|
||||
5 | 3 // 7 (OR)
|
||||
5 ^ 3 // 6 (XOR)
|
||||
~0 // -1 (NOT)
|
||||
1 << 3 // 8 (left shift)
|
||||
8 >> 3 // 1 (right shift)
|
||||
-1 >>> 1 // 2147483647 (unsigned right shift)
|
||||
```
|
||||
|
||||
### Unary
|
||||
|
||||
```javascript
|
||||
+5 // 5
|
||||
-5 // -5
|
||||
-(-5) // 5
|
||||
```
|
||||
|
||||
### Increment and Decrement
|
||||
|
||||
```javascript
|
||||
var x = 5
|
||||
x++ // returns 5, x becomes 6 (postfix)
|
||||
++x // returns 7, x becomes 7 (prefix)
|
||||
x-- // returns 7, x becomes 6 (postfix)
|
||||
--x // returns 5, x becomes 5 (prefix)
|
||||
```
|
||||
|
||||
### Compound Assignment
|
||||
|
||||
```javascript
|
||||
var x = 10
|
||||
x += 3 // 13
|
||||
x -= 3 // 10
|
||||
x *= 2 // 20
|
||||
x /= 4 // 5
|
||||
x %= 3 // 2
|
||||
```
|
||||
|
||||
### Ternary
|
||||
|
||||
```javascript
|
||||
var a = true ? 1 : 2 // 1
|
||||
var b = false ? 1 : 2 // 2
|
||||
var c = true ? (false ? 1 : 2) : 3 // 2 (nested)
|
||||
```
|
||||
|
||||
### Comma
|
||||
|
||||
The comma operator evaluates all expressions and returns the last.
|
||||
|
||||
```javascript
|
||||
var x = (1, 2, 3) // 3
|
||||
```
|
||||
|
||||
### In
|
||||
|
||||
Test whether a key exists in an object.
|
||||
|
||||
```javascript
|
||||
var o = {a: 1}
|
||||
"a" in o // true
|
||||
"b" in o // false
|
||||
```
|
||||
|
||||
### Delete
|
||||
|
||||
Remove a key from an object.
|
||||
|
||||
```javascript
|
||||
var o = {a: 1, b: 2}
|
||||
delete o.a
|
||||
"a" in o // false
|
||||
o.b // 2
|
||||
```
|
||||
|
||||
## Property Access
|
||||
|
||||
### Dot and Bracket
|
||||
|
||||
```javascript
|
||||
var o = {x: 10}
|
||||
o.x // 10 (dot read)
|
||||
o.x = 20 // dot write
|
||||
o["x"] // 20 (bracket read)
|
||||
var key = "x"
|
||||
o[key] // 20 (computed bracket)
|
||||
o["y"] = 30 // bracket write
|
||||
```
|
||||
|
||||
### Object as Key
|
||||
|
||||
Objects can be used as keys in other objects.
|
||||
|
||||
```javascript
|
||||
var k = {}
|
||||
var o = {}
|
||||
o[k] = 42
|
||||
o[k] // 42
|
||||
o[{}] // null (different object)
|
||||
k in o // true
|
||||
delete o[k]
|
||||
k in o // false
|
||||
```
|
||||
|
||||
### Chained Access
|
||||
|
||||
```javascript
|
||||
var d = {a: {b: [1, {c: 99}]}}
|
||||
d.a.b[1].c // 99
|
||||
```
|
||||
|
||||
## Arrays
|
||||
|
||||
Arrays are **distinct from objects**. They are ordered, numerically-indexed sequences.
|
||||
|
||||
```javascript
|
||||
var arr = [1, 2, 3]
|
||||
arr[0] // 1
|
||||
arr[2] = 10 // [1, 2, 10]
|
||||
length(arr) // 3
|
||||
```
|
||||
|
||||
### Push and Pop
|
||||
|
||||
```javascript
|
||||
var a = [1, 2]
|
||||
a[] = 3 // push: [1, 2, 3]
|
||||
length(a) // 3
|
||||
var v = a[] // pop: v is 3, a is [1, 2]
|
||||
length(a) // 2
|
||||
```
|
||||
|
||||
## Objects
|
||||
|
||||
Objects are key-value records with prototype-based inheritance.
|
||||
|
||||
```javascript
|
||||
var point = {x: 10, y: 20}
|
||||
point.x // 10
|
||||
point["y"] // 20
|
||||
```
|
||||
|
||||
### Prototypes
|
||||
|
||||
```javascript
|
||||
// Create object with prototype
|
||||
var parent = {x: 10}
|
||||
var child = meme(parent)
|
||||
child.x // 10 (inherited)
|
||||
proto(child) // parent
|
||||
|
||||
// Override does not mutate parent
|
||||
child.x = 20
|
||||
parent.x // 10
|
||||
```
|
||||
|
||||
### Mixins
|
||||
|
||||
```javascript
|
||||
var p = {a: 1}
|
||||
var m1 = {b: 2}
|
||||
var m2 = {c: 3}
|
||||
var child = meme(p, [m1, m2])
|
||||
child.a // 1 (from prototype)
|
||||
child.b // 2 (from mixin)
|
||||
child.c // 3 (from mixin)
|
||||
```
|
||||
|
||||
## Control Flow
|
||||
|
||||
### If / Else
|
||||
|
||||
```javascript
|
||||
var x = 0
|
||||
if (true) x = 1
|
||||
if (false) x = 2 else x = 3
|
||||
if (false) x = 4
|
||||
else if (true) x = 5
|
||||
else x = 6
|
||||
```
|
||||
|
||||
### While
|
||||
|
||||
```javascript
|
||||
var i = 0
|
||||
while (i < 5) i++
|
||||
|
||||
// break
|
||||
i = 0
|
||||
while (true) {
|
||||
if (i >= 3) break
|
||||
i++
|
||||
}
|
||||
|
||||
// continue
|
||||
var sum = 0
|
||||
i = 0
|
||||
while (i < 5) {
|
||||
i++
|
||||
if (i % 2 == 0) continue
|
||||
sum += i
|
||||
}
|
||||
```
|
||||
|
||||
### For
|
||||
|
||||
Variables cannot be declared in the for initializer. Declare them at the function body level.
|
||||
|
||||
```javascript
|
||||
var sum = 0
|
||||
var i = 0
|
||||
for (i = 0; i < 5; i++) sum += i
|
||||
|
||||
// break
|
||||
sum = 0
|
||||
i = 0
|
||||
for (i = 0; i < 10; i++) {
|
||||
if (i == 5) break
|
||||
sum += i
|
||||
}
|
||||
|
||||
// continue
|
||||
sum = 0
|
||||
i = 0
|
||||
for (i = 0; i < 5; i++) {
|
||||
if (i % 2 == 0) continue
|
||||
sum += i
|
||||
}
|
||||
|
||||
// nested
|
||||
sum = 0
|
||||
var j = 0
|
||||
for (i = 0; i < 3; i++) {
|
||||
for (j = 0; j < 3; j++) {
|
||||
sum++
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Functions
|
||||
|
||||
### Function Expressions
|
||||
|
||||
```javascript
|
||||
var add = function(a, b) { return a + b }
|
||||
add(2, 3) // 5
|
||||
```
|
||||
|
||||
### Arrow Functions
|
||||
|
||||
```javascript
|
||||
var double = x => x * 2
|
||||
double(5) // 10
|
||||
|
||||
var sum = (a, b) => a + b
|
||||
sum(2, 3) // 5
|
||||
|
||||
var block = x => {
|
||||
var y = x * 2
|
||||
return y + 1
|
||||
}
|
||||
block(5) // 11
|
||||
```
|
||||
|
||||
### Return
|
||||
|
||||
A function with no `return` returns `null`. An early `return` exits immediately.
|
||||
|
||||
```javascript
|
||||
var fn = function() { var x = 1 }
|
||||
fn() // null
|
||||
|
||||
var fn2 = function() { return 1; return 2 }
|
||||
fn2() // 1
|
||||
```
|
||||
|
||||
### Arguments
|
||||
|
||||
Extra arguments are ignored. Missing arguments are `null`.
|
||||
|
||||
```javascript
|
||||
var fn = function(a, b) { return a + b }
|
||||
fn(1, 2, 3) // 3 (extra arg ignored)
|
||||
|
||||
var fn2 = function(a, b) { return a }
|
||||
fn2(1) // 1 (b is null)
|
||||
```
|
||||
|
||||
### Immediately Invoked Function Expression
|
||||
|
||||
```javascript
|
||||
var r = (function(x) { return x * 2 })(21) // 42
|
||||
```
|
||||
|
||||
### Closures
|
||||
|
||||
Functions capture variables from their enclosing scope.
|
||||
|
||||
```javascript
|
||||
var make = function(x) {
|
||||
return function(y) { return x + y }
|
||||
}
|
||||
var add5 = make(5)
|
||||
add5(3) // 8
|
||||
```
|
||||
|
||||
Captured variables can be mutated:
|
||||
|
||||
```javascript
|
||||
var counter = function() {
|
||||
var n = 0
|
||||
return function() { n = n + 1; return n }
|
||||
}
|
||||
var c = counter()
|
||||
c() // 1
|
||||
c() // 2
|
||||
```
|
||||
|
||||
### Recursion
|
||||
|
||||
```javascript
|
||||
var fact = function(n) {
|
||||
if (n <= 1) return 1
|
||||
return n * fact(n - 1)
|
||||
}
|
||||
fact(5) // 120
|
||||
```
|
||||
|
||||
### This Binding
|
||||
|
||||
When a function is called as a method, `this` refers to the object.
|
||||
|
||||
```javascript
|
||||
var obj = {
|
||||
val: 10,
|
||||
get: function() { return this.val }
|
||||
}
|
||||
obj.get() // 10
|
||||
```
|
||||
|
||||
### Currying
|
||||
|
||||
```javascript
|
||||
var f = function(a) {
|
||||
return function(b) {
|
||||
return function(c) { return a + b + c }
|
||||
}
|
||||
}
|
||||
f(1)(2)(3) // 6
|
||||
```
|
||||
|
||||
## Identifiers
|
||||
|
||||
Identifiers can contain `?` and `!` characters, both as suffixes and mid-name.
|
||||
|
||||
```javascript
|
||||
var nil? = (x) => x == null
|
||||
nil?(null) // true
|
||||
nil?(42) // false
|
||||
|
||||
var set! = (x) => x + 1
|
||||
set!(5) // 6
|
||||
|
||||
var is?valid = (x) => x > 0
|
||||
is?valid(3) // true
|
||||
|
||||
var do!stuff = () => 42
|
||||
do!stuff() // 42
|
||||
```
|
||||
|
||||
The `?` in an identifier is not confused with the ternary operator:
|
||||
|
||||
```javascript
|
||||
var nil? = (x) => x == null
|
||||
var a = nil?(null) ? "yes" : "no" // "yes"
|
||||
```
|
||||
|
||||
## Type Checking
|
||||
|
||||
### Type Functions
|
||||
|
||||
```javascript
|
||||
is_number(42) // true
|
||||
is_text("hi") // true
|
||||
is_logical(true) // true
|
||||
is_object({}) // true
|
||||
is_array([]) // true
|
||||
is_function(function(){}) // true
|
||||
is_null(null) // true
|
||||
is_object([]) // false (array is not object)
|
||||
is_array({}) // false (object is not array)
|
||||
```
|
||||
|
||||
### Truthiness
|
||||
|
||||
Falsy values: `false`, `0`, `""`, `null`. Everything else is truthy.
|
||||
|
||||
```javascript
|
||||
if (0) ... // not entered
|
||||
if ("") ... // not entered
|
||||
if (null) ... // not entered
|
||||
if (1) ... // entered
|
||||
if ("hi") ... // entered
|
||||
if ({}) ... // entered
|
||||
if ([]) ... // entered
|
||||
```
|
||||
|
||||
## Immutability with Stone
|
||||
|
||||
The `stone()` function makes values permanently immutable.
|
||||
|
||||
```javascript
|
||||
var o = {x: 1}
|
||||
is_stone(o) // false
|
||||
stone(o)
|
||||
is_stone(o) // true
|
||||
o.x = 2 // disrupts!
|
||||
```
|
||||
|
||||
Stone is **deep** — all nested objects and arrays are also frozen. This cannot be reversed.
|
||||
|
||||
## Function Proxy
|
||||
|
||||
A function with two parameters (`name`, `args`) acts as a proxy when properties are accessed on it. Any method call on the function dispatches through the proxy.
|
||||
|
||||
```javascript
|
||||
var proxy = function(name, args) {
|
||||
return `${name}:${length(args)}`
|
||||
}
|
||||
proxy.hello() // "hello:0"
|
||||
proxy.add(1, 2) // "add:2"
|
||||
proxy["method"]() // "method:0"
|
||||
var m = "dynamic"
|
||||
proxy[m]() // "dynamic:0"
|
||||
```
|
||||
|
||||
For non-proxy functions, property access disrupts:
|
||||
|
||||
```javascript
|
||||
var fn = function() { return 1 }
|
||||
fn.foo // disrupts
|
||||
fn.foo = 1 // disrupts
|
||||
```
|
||||
|
||||
## Regex
|
||||
|
||||
Regex literals are written with forward slashes, with optional flags.
|
||||
|
||||
```javascript
|
||||
var r = /\d+/
|
||||
var result = extract("abc123", r)
|
||||
result[0] // "123"
|
||||
|
||||
var ri = /hello/i
|
||||
var result2 = extract("Hello", ri)
|
||||
result2[0] // "Hello"
|
||||
```
|
||||
|
||||
## Error Handling
|
||||
|
||||
ƿit uses `disrupt` and `disruption` for error handling. A `disrupt` signals that something went wrong. The `disruption` block attached to a function catches it.
|
||||
|
||||
```javascript
|
||||
var safe_divide = function(a, b) {
|
||||
if (b == 0) disrupt
|
||||
return a / b
|
||||
} disruption {
|
||||
print("something went wrong")
|
||||
}
|
||||
```
|
||||
|
||||
`disrupt` is a bare keyword — it does not carry a value. The `disruption` block knows that something went wrong, but not what.
|
||||
|
||||
### Re-raising
|
||||
|
||||
A `disruption` block can re-raise by calling `disrupt` again:
|
||||
|
||||
```javascript
|
||||
var outer = function() {
|
||||
var inner = function() { disrupt } disruption { disrupt }
|
||||
inner()
|
||||
} disruption {
|
||||
// caught here after re-raise
|
||||
}
|
||||
outer()
|
||||
```
|
||||
|
||||
### Testing for Disruption
|
||||
|
||||
```javascript
|
||||
var should_disrupt = function(fn) {
|
||||
var caught = false
|
||||
var wrapper = function() {
|
||||
fn()
|
||||
} disruption {
|
||||
caught = true
|
||||
}
|
||||
wrapper()
|
||||
return caught
|
||||
}
|
||||
```
|
||||
|
||||
If an actor has an unhandled disruption, it crashes.
|
||||
|
||||
## Self-Referencing Structures
|
||||
|
||||
Objects can reference themselves:
|
||||
|
||||
```javascript
|
||||
var o = {name: "root"}
|
||||
o.self = o
|
||||
o.self.self.name // "root"
|
||||
```
|
||||
|
||||
## Variable Shadowing
|
||||
|
||||
Inner functions can shadow outer variables:
|
||||
|
||||
```javascript
|
||||
var x = 10
|
||||
var fn = function() {
|
||||
var x = 20
|
||||
return x
|
||||
}
|
||||
fn() // 20
|
||||
x // 10
|
||||
```
|
||||
@@ -1,10 +0,0 @@
|
||||
nav:
|
||||
- text.md
|
||||
- number.md
|
||||
- array.md
|
||||
- object.md
|
||||
- blob.md
|
||||
- time.md
|
||||
- math.md
|
||||
- json.md
|
||||
- random.md
|
||||
18
docs/library/_index.md
Normal file
18
docs/library/_index.md
Normal file
@@ -0,0 +1,18 @@
|
||||
---
|
||||
title: "Standard Library"
|
||||
description: "ƿit standard library modules"
|
||||
weight: 90
|
||||
type: "docs"
|
||||
---
|
||||
|
||||
The standard library provides modules loaded with `use()`.
|
||||
|
||||
| Module | Description |
|
||||
|--------|-------------|
|
||||
| [blob](/docs/library/blob/) | Binary data (bits, not bytes) |
|
||||
| [time](/docs/library/time/) | Time constants and conversions |
|
||||
| [math](/docs/library/math/) | Trigonometry, logarithms, roots |
|
||||
| [json](/docs/library/json/) | JSON encoding and decoding |
|
||||
| [random](/docs/library/random/) | Random number generation |
|
||||
|
||||
The `text`, `number`, `array`, and `object` functions are intrinsics — they are always available without `use`. See [Built-in Functions](/docs/functions/) for the full list, and the individual reference pages for [text](/docs/library/text/), [number](/docs/library/number/), [array](/docs/library/array/), and [object](/docs/library/object/).
|
||||
@@ -1,12 +1,19 @@
|
||||
# array
|
||||
---
|
||||
title: "array"
|
||||
description: "Array creation and manipulation"
|
||||
weight: 30
|
||||
type: "docs"
|
||||
---
|
||||
|
||||
The `array` function and its methods handle array creation and manipulation.
|
||||
The `array` function is an intrinsic (always available, no `use()` needed). It is **polymorphic** — its behavior depends on the type of the first argument.
|
||||
|
||||
## Creation
|
||||
## From a Number
|
||||
|
||||
Create an array of a given size.
|
||||
|
||||
### array(number)
|
||||
|
||||
Create an array of specified size, filled with `null`.
|
||||
All elements initialized to `null`.
|
||||
|
||||
```javascript
|
||||
array(3) // [null, null, null]
|
||||
@@ -14,24 +21,36 @@ array(3) // [null, null, null]
|
||||
|
||||
### array(number, initial)
|
||||
|
||||
Create an array with initial values.
|
||||
All elements initialized to a value. If initial is a function, it is called for each element (passed the index if arity >= 1).
|
||||
|
||||
```javascript
|
||||
array(3, 0) // [0, 0, 0]
|
||||
array(3, i => i * 2) // [0, 2, 4]
|
||||
```
|
||||
|
||||
## From an Array
|
||||
|
||||
Copy, map, concat, or slice.
|
||||
|
||||
### array(array)
|
||||
|
||||
Copy an array.
|
||||
Copy an array (mutable).
|
||||
|
||||
```javascript
|
||||
var copy = array(original)
|
||||
```
|
||||
|
||||
### array(array, function)
|
||||
|
||||
Map — call function with each element, collect results.
|
||||
|
||||
```javascript
|
||||
array([1, 2, 3], x => x * 2) // [2, 4, 6]
|
||||
```
|
||||
|
||||
### array(array, from, to)
|
||||
|
||||
Slice an array.
|
||||
Slice — extract a sub-array. Negative indices count from end.
|
||||
|
||||
```javascript
|
||||
array([1, 2, 3, 4, 5], 1, 4) // [2, 3, 4]
|
||||
@@ -40,32 +59,36 @@ array([1, 2, 3], -2) // [2, 3]
|
||||
|
||||
### array(array, another)
|
||||
|
||||
Concatenate arrays.
|
||||
Concatenate two arrays.
|
||||
|
||||
```javascript
|
||||
array([1, 2], [3, 4]) // [1, 2, 3, 4]
|
||||
```
|
||||
|
||||
### array(object)
|
||||
## From a Record
|
||||
|
||||
Get keys of an object.
|
||||
### array(record)
|
||||
|
||||
Get the keys of a record as an array of text.
|
||||
|
||||
```javascript
|
||||
array({a: 1, b: 2}) // ["a", "b"]
|
||||
```
|
||||
|
||||
## From Text
|
||||
|
||||
### array(text)
|
||||
|
||||
Split text into grapheme clusters.
|
||||
Split text into individual characters (grapheme clusters). This is the standard way to iterate over characters in a string.
|
||||
|
||||
```javascript
|
||||
array("hello") // ["h", "e", "l", "l", "o"]
|
||||
array("👨👩👧") // ["👨👩👧"]
|
||||
array("hello") // ["h", "e", "l", "l", "o"]
|
||||
array("ƿit") // ["ƿ", "i", "t"]
|
||||
```
|
||||
|
||||
### array(text, separator)
|
||||
|
||||
Split text by separator.
|
||||
Split text by a separator string.
|
||||
|
||||
```javascript
|
||||
array("a,b,c", ",") // ["a", "b", "c"]
|
||||
@@ -73,7 +96,7 @@ array("a,b,c", ",") // ["a", "b", "c"]
|
||||
|
||||
### array(text, length)
|
||||
|
||||
Split text into chunks.
|
||||
Dice text into chunks of a given length.
|
||||
|
||||
```javascript
|
||||
array("abcdef", 2) // ["ab", "cd", "ef"]
|
||||
@@ -87,13 +110,13 @@ Iterate over elements.
|
||||
|
||||
```javascript
|
||||
array.for([1, 2, 3], function(el, i) {
|
||||
log.console(i, el)
|
||||
print(i, el)
|
||||
})
|
||||
|
||||
// With early exit
|
||||
array.for([1, 2, 3, 4], function(el) {
|
||||
if (el > 2) return true
|
||||
log.console(el)
|
||||
print(el)
|
||||
}, false, true) // prints 1, 2
|
||||
```
|
||||
|
||||
|
||||
@@ -1,4 +1,9 @@
|
||||
# blob
|
||||
---
|
||||
title: "blob"
|
||||
description: "Binary data containers (bits, not bytes)"
|
||||
weight: 50
|
||||
type: "docs"
|
||||
---
|
||||
|
||||
Blobs are binary large objects — containers of bits (not bytes). They're used for encoding data, messages, images, network payloads, and more.
|
||||
|
||||
|
||||
@@ -1,4 +1,9 @@
|
||||
# json
|
||||
---
|
||||
title: "json"
|
||||
description: "JSON encoding and decoding"
|
||||
weight: 80
|
||||
type: "docs"
|
||||
---
|
||||
|
||||
JSON encoding and decoding.
|
||||
|
||||
@@ -86,5 +91,5 @@ var config_text = json.encode(config, 2)
|
||||
|
||||
// Load configuration
|
||||
var loaded = json.decode(config_text)
|
||||
log.console(loaded.debug) // true
|
||||
print(loaded.debug) // true
|
||||
```
|
||||
|
||||
@@ -1,10 +1,15 @@
|
||||
# math
|
||||
---
|
||||
title: "math"
|
||||
description: "Trigonometry, logarithms, and roots"
|
||||
weight: 70
|
||||
type: "docs"
|
||||
---
|
||||
|
||||
Cell provides three math modules with identical functions but different angle representations:
|
||||
ƿit provides three math modules with identical functions but different angle representations:
|
||||
|
||||
```javascript
|
||||
var math = use('math/radians') // angles in radians
|
||||
var math = use('math/degrees') // angles in degrees
|
||||
var math = use('math/degrees') // angles in degrees
|
||||
var math = use('math/cycles') // angles in cycles (0-1)
|
||||
```
|
||||
|
||||
@@ -35,7 +40,7 @@ math.tangent(math.pi / 4) // 1 (radians)
|
||||
Inverse sine.
|
||||
|
||||
```javascript
|
||||
math.arc_sine(1) // π/2 (radians)
|
||||
math.arc_sine(1) // pi/2 (radians)
|
||||
```
|
||||
|
||||
### arc_cosine(n)
|
||||
@@ -43,7 +48,7 @@ math.arc_sine(1) // π/2 (radians)
|
||||
Inverse cosine.
|
||||
|
||||
```javascript
|
||||
math.arc_cosine(0) // π/2 (radians)
|
||||
math.arc_cosine(0) // pi/2 (radians)
|
||||
```
|
||||
|
||||
### arc_tangent(n, denominator)
|
||||
@@ -51,9 +56,9 @@ math.arc_cosine(0) // π/2 (radians)
|
||||
Inverse tangent. With two arguments, computes atan2.
|
||||
|
||||
```javascript
|
||||
math.arc_tangent(1) // π/4 (radians)
|
||||
math.arc_tangent(1, 1) // π/4 (radians)
|
||||
math.arc_tangent(-1, -1) // -3π/4 (radians)
|
||||
math.arc_tangent(1) // pi/4 (radians)
|
||||
math.arc_tangent(1, 1) // pi/4 (radians)
|
||||
math.arc_tangent(-1, -1) // -3pi/4 (radians)
|
||||
```
|
||||
|
||||
## Exponentials and Logarithms
|
||||
@@ -64,7 +69,7 @@ Euler's number raised to a power. Default power is 1.
|
||||
|
||||
```javascript
|
||||
math.e() // 2.718281828...
|
||||
math.e(2) // e²
|
||||
math.e(2) // e^2
|
||||
```
|
||||
|
||||
### ln(n)
|
||||
@@ -130,21 +135,21 @@ math.e() // 2.71828...
|
||||
var math = use('math/radians')
|
||||
|
||||
// Distance between two points
|
||||
function distance(x1, y1, x2, y2) {
|
||||
var distance = function(x1, y1, x2, y2) {
|
||||
var dx = x2 - x1
|
||||
var dy = y2 - y1
|
||||
return math.sqrt(dx * dx + dy * dy)
|
||||
}
|
||||
|
||||
// Angle between two points
|
||||
function angle(x1, y1, x2, y2) {
|
||||
var angle = function(x1, y1, x2, y2) {
|
||||
return math.arc_tangent(y2 - y1, x2 - x1)
|
||||
}
|
||||
|
||||
// Rotate a point
|
||||
function rotate(x, y, angle) {
|
||||
var c = math.cosine(angle)
|
||||
var s = math.sine(angle)
|
||||
var rotate = function(x, y, a) {
|
||||
var c = math.cosine(a)
|
||||
var s = math.sine(a)
|
||||
return {
|
||||
x: x * c - y * s,
|
||||
y: x * s + y * c
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
# number
|
||||
---
|
||||
title: "number"
|
||||
description: "Numeric conversion and operations"
|
||||
weight: 20
|
||||
type: "docs"
|
||||
---
|
||||
|
||||
The `number` function and its methods handle numeric conversion and operations.
|
||||
The `number` function is an intrinsic (always available, no `use()` needed). It is **polymorphic** — its behavior depends on the type of the first argument.
|
||||
|
||||
## Conversion
|
||||
|
||||
@@ -29,15 +34,15 @@ Parse formatted numbers.
|
||||
|
||||
| Format | Description |
|
||||
|--------|-------------|
|
||||
| `""` | Standard decimal |
|
||||
| `"u"` | Underbar separator (1_000) |
|
||||
| `"d"` | Comma separator (1,000) |
|
||||
| `"s"` | Space separator (1 000) |
|
||||
| `"v"` | European (1.000,50) |
|
||||
| `"b"` | Binary |
|
||||
| `"o"` | Octal |
|
||||
| `"h"` | Hexadecimal |
|
||||
| `"j"` | JavaScript style (0x, 0o, 0b prefixes) |
|
||||
| `""` | Standard decimal |
|
||||
| `"u"` | Underbar separator (1_000) |
|
||||
| `"d"` | Comma separator (1,000) |
|
||||
| `"s"` | Space separator (1 000) |
|
||||
| `"v"` | European (1.000,50) |
|
||||
| `"b"` | Binary |
|
||||
| `"o"` | Octal |
|
||||
| `"h"` | Hexadecimal |
|
||||
| `"j"` | JavaScript style (0x, 0o, 0b prefixes) |
|
||||
|
||||
```javascript
|
||||
number("1,000", "d") // 1000
|
||||
@@ -118,20 +123,20 @@ Get the fractional part.
|
||||
fraction(4.75) // 0.75
|
||||
```
|
||||
|
||||
### min(...values)
|
||||
### min(a, b)
|
||||
|
||||
Return the smallest value.
|
||||
Return the smaller of two numbers.
|
||||
|
||||
```javascript
|
||||
min(3, 1, 4, 1, 5) // 1
|
||||
min(3, 5) // 3
|
||||
```
|
||||
|
||||
### max(...values)
|
||||
### max(a, b)
|
||||
|
||||
Return the largest value.
|
||||
Return the larger of two numbers.
|
||||
|
||||
```javascript
|
||||
max(3, 1, 4, 1, 5) // 5
|
||||
max(3, 5) // 5
|
||||
```
|
||||
|
||||
### remainder(dividend, divisor)
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
# object
|
||||
---
|
||||
title: "object"
|
||||
description: "Object creation and manipulation"
|
||||
weight: 40
|
||||
type: "docs"
|
||||
---
|
||||
|
||||
The `object` function and related utilities handle object creation and manipulation.
|
||||
The `object` function is an intrinsic (always available, no `use()` needed). It is **polymorphic** — its behavior depends on the types of its arguments.
|
||||
|
||||
## Creation
|
||||
## From a Record
|
||||
|
||||
### object(obj)
|
||||
|
||||
@@ -29,6 +34,8 @@ Select specific keys.
|
||||
object({a: 1, b: 2, c: 3}, ["a", "c"]) // {a: 1, c: 3}
|
||||
```
|
||||
|
||||
## From an Array of Keys
|
||||
|
||||
### object(keys)
|
||||
|
||||
Create object from keys (values are `true`).
|
||||
@@ -60,9 +67,9 @@ object(["a", "b", "c"], (k, i) => i) // {a: 0, b: 1, c: 2}
|
||||
Create a new object with the given prototype.
|
||||
|
||||
```javascript
|
||||
var animal = {speak: function() { log.console("...") }}
|
||||
var animal = {speak: function() { print("...") }}
|
||||
var dog = meme(animal)
|
||||
dog.speak = function() { log.console("woof") }
|
||||
dog.speak = function() { print("woof") }
|
||||
```
|
||||
|
||||
### proto(obj)
|
||||
@@ -104,9 +111,4 @@ var obj = {a: 1, b: 2, c: 3}
|
||||
|
||||
// Get all keys
|
||||
var keys = array(obj) // ["a", "b", "c"]
|
||||
|
||||
// Iterate
|
||||
for (var key in obj) {
|
||||
log.console(key, obj[key])
|
||||
}
|
||||
```
|
||||
|
||||
@@ -1,4 +1,9 @@
|
||||
# random
|
||||
---
|
||||
title: "random"
|
||||
description: "Random number generation"
|
||||
weight: 90
|
||||
type: "docs"
|
||||
---
|
||||
|
||||
Random number generation.
|
||||
|
||||
@@ -43,7 +48,7 @@ var random = use('random')
|
||||
var coin_flip = random.random() < 0.5
|
||||
|
||||
// Random element from array
|
||||
function pick(arr) {
|
||||
var pick = function(arr) {
|
||||
return arr[random.random_whole(length(arr))]
|
||||
}
|
||||
|
||||
@@ -51,11 +56,14 @@ var colors = ["red", "green", "blue"]
|
||||
var color = pick(colors)
|
||||
|
||||
// Shuffle array
|
||||
function shuffle(arr) {
|
||||
var shuffle = function(arr) {
|
||||
var result = array(arr) // copy
|
||||
for (var i = length(result) - 1; i > 0; i--) {
|
||||
var j = random.random_whole(i + 1)
|
||||
var temp = result[i]
|
||||
var i = length(result) - 1
|
||||
var j = 0
|
||||
var temp = null
|
||||
for (i = length(result) - 1; i > 0; i--) {
|
||||
j = random.random_whole(i + 1)
|
||||
temp = result[i]
|
||||
result[i] = result[j]
|
||||
result[j] = temp
|
||||
}
|
||||
@@ -63,8 +71,8 @@ function shuffle(arr) {
|
||||
}
|
||||
|
||||
// Random in range
|
||||
function random_range(min, max) {
|
||||
return min + random.random() * (max - min)
|
||||
var random_range = function(lo, hi) {
|
||||
return lo + random.random() * (hi - lo)
|
||||
}
|
||||
|
||||
var x = random_range(-10, 10) // -10 to 10
|
||||
|
||||
@@ -1,19 +1,28 @@
|
||||
# text
|
||||
---
|
||||
title: "text"
|
||||
description: "String conversion and manipulation"
|
||||
weight: 10
|
||||
type: "docs"
|
||||
---
|
||||
|
||||
The `text` function and its methods handle string conversion and manipulation.
|
||||
The `text` function is an intrinsic (always available, no `use()` needed). It is **polymorphic** — its behavior depends on the type of the first argument.
|
||||
|
||||
## Conversion
|
||||
To split text into characters, use `array(text)` — see [array](/docs/library/array/).
|
||||
|
||||
## From an Array
|
||||
|
||||
### text(array, separator)
|
||||
|
||||
Convert an array to text, joining elements with a separator (default: space).
|
||||
Join array elements into text with a separator (default: empty string).
|
||||
|
||||
```javascript
|
||||
text([1, 2, 3]) // "1 2 3"
|
||||
text([1, 2, 3], ", ") // "1, 2, 3"
|
||||
text(["a", "b"], "-") // "a-b"
|
||||
text(["h", "e", "l", "l", "o"]) // "hello"
|
||||
text([1, 2, 3], ", ") // "1, 2, 3"
|
||||
text(["a", "b"], "-") // "a-b"
|
||||
```
|
||||
|
||||
## From a Number
|
||||
|
||||
### text(number, radix)
|
||||
|
||||
Convert a number to text. Radix is 2-36 (default: 10).
|
||||
@@ -24,13 +33,16 @@ text(255, 16) // "ff"
|
||||
text(255, 2) // "11111111"
|
||||
```
|
||||
|
||||
## From Text
|
||||
|
||||
### text(text, from, to)
|
||||
|
||||
Extract a substring from index `from` to `to`.
|
||||
Extract a substring from index `from` to `to`. Negative indices count from end.
|
||||
|
||||
```javascript
|
||||
text("hello world", 0, 5) // "hello"
|
||||
text("hello world", 6) // "world"
|
||||
text("hello", -3) // "llo"
|
||||
```
|
||||
|
||||
## Methods
|
||||
@@ -101,7 +113,7 @@ text.format("{0} + {1} = {2}", [1, 2, 3])
|
||||
Unicode normalize the text (NFC form).
|
||||
|
||||
```javascript
|
||||
text.normalize("café") // normalized form
|
||||
text.normalize("cafe\u0301") // normalized form
|
||||
```
|
||||
|
||||
### text.codepoint(text)
|
||||
@@ -109,8 +121,7 @@ text.normalize("café") // normalized form
|
||||
Get the Unicode codepoint of the first character.
|
||||
|
||||
```javascript
|
||||
text.codepoint("A") // 65
|
||||
text.codepoint("😀") // 128512
|
||||
text.codepoint("A") // 65
|
||||
```
|
||||
|
||||
### text.extract(text, pattern, from, to)
|
||||
|
||||
@@ -1,4 +1,9 @@
|
||||
# time
|
||||
---
|
||||
title: "time"
|
||||
description: "Time constants and conversion functions"
|
||||
weight: 60
|
||||
type: "docs"
|
||||
---
|
||||
|
||||
The time module provides time constants and conversion functions.
|
||||
|
||||
@@ -96,7 +101,7 @@ var last_week = now - time.week
|
||||
var later = now + (2 * time.hour)
|
||||
|
||||
// Format future time
|
||||
log.console(time.text(tomorrow))
|
||||
print(time.text(tomorrow))
|
||||
```
|
||||
|
||||
## Example
|
||||
@@ -108,9 +113,9 @@ var time = use('time')
|
||||
var start = time.number()
|
||||
// ... do work ...
|
||||
var elapsed = time.number() - start
|
||||
log.console(`Took ${elapsed} seconds`)
|
||||
print(`Took ${elapsed} seconds`)
|
||||
|
||||
// Schedule for tomorrow
|
||||
var tomorrow = time.number() + time.day
|
||||
log.console(`Tomorrow: ${time.text(tomorrow, "yyyy-MM-dd")}`)
|
||||
print(`Tomorrow: ${time.text(tomorrow, "yyyy-MM-dd")}`)
|
||||
```
|
||||
|
||||
248
docs/memory.md
248
docs/memory.md
@@ -1,248 +0,0 @@
|
||||
# Cell actor scripting language
|
||||
|
||||
Cell is a Misty [https://mistysystem.com](https://mistysystem.com) implementation.
|
||||
|
||||
## Memory
|
||||
Values are 32 bit for 32 bit builds and 64 bit for 64 bit builds.
|
||||
|
||||
### 32 bit value
|
||||
|
||||
LSB = 0
|
||||
payload is a 31 bit signed int
|
||||
|
||||
LSB = 01
|
||||
payload is a 30 bit pointer
|
||||
|
||||
LSB = 11
|
||||
next 3 bits = special tag. 27 bits of payload.
|
||||
|
||||
### 64 bit value
|
||||
LSB = 0
|
||||
payload is a 32 bit signed int, using high 32 bits
|
||||
|
||||
LSB = 01
|
||||
payload is a 61 bit pointer
|
||||
|
||||
LSB = 101
|
||||
Short float: a 61 bit double, with 3 less exponent bits
|
||||
|
||||
LSB = 11
|
||||
Special tag: next 3 bits. 5 bits total. 59 bits of payload. 8 total special tags.
|
||||
|
||||
Special tags:
|
||||
1: Bool. Payload is 0 or 1.
|
||||
2: null. payload is 0.
|
||||
3: exception.
|
||||
4: string.
|
||||
Immediate string. Next 3 low bits = length in bytes. Rest is string data. This allows for strings up to 7 ascii letters. Encoded in utf8.
|
||||
|
||||
## Numbers and math
|
||||
Cell can be compiled with different levels of exactness for numeracy. Any number which cannot be represented exactly becomes "null". Any numeric operation which includes "null" results in "null".
|
||||
|
||||
Using short floats in a 64 bit system means you have doubles in the range of +- 10^38, not the full range of double. If you create a number out of that range, it's null.
|
||||
|
||||
You can also compile a 64 bit system with full precision doubles, but this will use more memory and may be slower.
|
||||
|
||||
You can also compile a 64 bit system with 32 bit floats, stored as a 32 bit int is. Again, out of the 32 bit float range = null.
|
||||
|
||||
You can compile without floating point support at all; 32 bit ints are then used for fixed point calculations.
|
||||
|
||||
Or, you can compile using Dec64, which is a 64 bit decimal floating point format, for exact precision.
|
||||
|
||||
## Objects
|
||||
Objects are heap allocated, referenced by a pointer value. They are all preceded by an object header, the length of a word on the system.
|
||||
|
||||
### 64 bit build
|
||||
56 bits capacity
|
||||
1 bit memory reclamation flag: note that this obj has already been moved
|
||||
2 bit reserved (per object)
|
||||
1 bit stone: note that this obj is immutable
|
||||
3 bit type: note the type of the object
|
||||
1 bit: fwd: note that this obj is a forward linkage
|
||||
|
||||
Last bit ..1:
|
||||
The forward type indicates that the object (an array, blob, pretext, or record) has grown beyond its capacity and is now residing at a new address. The remaining 63 bits contain the address of the enlarged object. Forward linkages are cleaned up by the memory reclaimer.
|
||||
|
||||
Type 7: C light C object
|
||||
|
||||
Header
|
||||
Pointer
|
||||
|
||||
Capacity is an ID of a registered C type.
|
||||
Pointer is a pointer to the opaque C object.
|
||||
|
||||
Type 0: Array
|
||||
Header
|
||||
Length
|
||||
Element[]
|
||||
|
||||
Capacity is number of elements the array can hold. Length is number of elements in use. Number of words used by an array is capacity + 2.
|
||||
|
||||
Type 1: blob
|
||||
Header
|
||||
Length
|
||||
Bit[]
|
||||
Capacity is number of bits the blob can hold. Length is number of bits in use. Bits follow, from [0] to [capacity - 1], with [0] bit in the most significant position of word 2, and [63] in the least significant position of word 2. The last word is zero filled, if necessary.
|
||||
|
||||
Number of words used is (capacity + 63) // 64 + 2
|
||||
|
||||
Type 2: Text
|
||||
Text has two forms, depending on if it is stone or not, which changes the meaning of its length word.
|
||||
|
||||
Header
|
||||
Length(pretext) or Hash(text)
|
||||
Character[0] and character[1]
|
||||
|
||||
Capacity of pretex is the number of characters it can hold. During stoning and reclamation, capacity is set to the length.
|
||||
|
||||
The capacity of a text is its length.
|
||||
|
||||
The length of a pretext is the number of characters it contains; it is not greater than the capacity.
|
||||
|
||||
Hash of a text is used for organizing records. If the hash is zero, it's not been computed yet. All texts in the immutable memory have hashes.
|
||||
|
||||
A text object contains UTF32 characters, packed two per word. If the number of characters is odd, the least significant half of the last word is zero filled.
|
||||
|
||||
The number of words used by a text is (capacity + 1) // 2 + 2
|
||||
|
||||
Type 3: Record
|
||||
|
||||
A record is an array of fields represented as key/value pairs. Fields are located by hashes of texts, using open addressing with linear probing and lazy deletion. The load factor is less than 0.5.
|
||||
|
||||
Header
|
||||
Prototype
|
||||
Length
|
||||
Key[0]
|
||||
Value[0]
|
||||
Key[1]
|
||||
Value[1]
|
||||
...
|
||||
|
||||
The capacity is the number of fields the record can hold. It is a power of two minus one. It is at least twice the length.
|
||||
|
||||
The length is the number of fields that the record currently contains.
|
||||
|
||||
A field candidate number is identified by and(key.hash, capacity). In case of hash collision, advance to the next field. If this goes past the end, continue with field 1. Field 0 is reserved.
|
||||
|
||||
The "exception" special tag is used to mark deleted entries in the object map.
|
||||
|
||||
The number of words used by a record is (capacity + 1) * 2.
|
||||
|
||||
Prototypes are searched for for properties if one cannot be found on the record itself. Prototypes can have prototypes.
|
||||
|
||||
#### key[0] and value[0]
|
||||
These are reserved for internal use, and skipped over during key probing.
|
||||
|
||||
The first 32 bits of key are used as a 32 bit integer key, if this object has ever been used as a key itself.
|
||||
|
||||
The last 32 bits are used as an opaque C class key. C types can be registered with the system, and each are assigned a monotonically increasing number. In the case that this object has a C type, then the bottom 32 bits of key[0] are not 0. If that is the case, then a pointer to its C object is stored in value[0].
|
||||
|
||||
#### Valid keys & Hashing
|
||||
Keys are stored directly in object maps. There are three possibilities for a vaild key: an object text, an object record, or an immediate text.
|
||||
|
||||
In the case of an immediate text, the hash is computed on the fly using the fash64_hash_one function, before being used to look up the key in the object map. Direct value comparison is used to confirm the key.
|
||||
|
||||
For object texts (texts longer than 7 ascii chars), the hash is stored in the text object itself. When an object text is used as a key, a stone version is created and interned. Any program static texts reference this stoned, interned text. When looking up a heap text as a key, it is first discovered if it's in the interned table. If it's not, the key is not in the object (since all keys are interned). If it is, the interned version is returned to check against the object map. The hash of the interned text is used to look up the key in the object map, and then direct pointer comparison is used to confirm the key.
|
||||
|
||||
For record keys, these are unique; once a record is used as a key, it gets assigned a monotonically increasing 32 bit integer, stored in key[0]. When checking it in an object map, the integer is used directly as the key. If key[0] is 0, the record has not been used as a key yet. If it's not 0, fash64_hash_one is used to compute a hash of its ID, and then direct value pointer comparison is used to confirm.
|
||||
|
||||
### Text interning
|
||||
Texts that cannot fit in an immediate, and which are used as an object key, create a stoned and interned version (the pointer which is used as the key). Any text literals are also stoned and interned.
|
||||
|
||||
The interning table is an open addressed hash, with a load of 0.8, using a robin hood value. Probing is done using the text hash, confirmation is done using length, and then memcmp of the text.
|
||||
|
||||
When the GC run, a new interned text table is created. Each text literal, and each text used as a key, is added to the new table, as the live objects are copied. This keeps the interning table from becoming a graveyard. Interned values are never deleted until a GC.
|
||||
|
||||
Type 4: Function
|
||||
|
||||
Header
|
||||
Code
|
||||
Outer
|
||||
A function object has zero capacity and is always stone.
|
||||
|
||||
Code is a pointer to the code object that the function executes.
|
||||
|
||||
Outer is a pointer to the frame that created this function object.
|
||||
|
||||
Size is 3 words.
|
||||
|
||||
Type 5: Frame
|
||||
|
||||
Header
|
||||
Function
|
||||
Caller
|
||||
Return address
|
||||
|
||||
The activation frame is created when a function is invoked to hold its linkages and state.
|
||||
|
||||
The capacity is the number of slots, including the inputs, variables, temporaries, and the four words of overhead. A frame, unlike the other types, is never stone.
|
||||
|
||||
The function is the address of the function object being called.
|
||||
|
||||
The caller is the address of the frame that is invoking the function.
|
||||
|
||||
The return address is the address of the instruction in the code that should be executed upon return.
|
||||
|
||||
Next come the input arguments, if any.
|
||||
|
||||
Then the variables closed over by the inner functions.
|
||||
|
||||
Then the variables that are not closed over, followed by the temporaries.
|
||||
|
||||
When a function returns, the caller is set to zero. This is a signal to the memory reclaimer that the frame can be reduced.
|
||||
|
||||
Type 6: Code
|
||||
|
||||
Header
|
||||
Arity
|
||||
Size
|
||||
Closure size
|
||||
Entry point
|
||||
Disruption point
|
||||
|
||||
A code object exists in the actor's immutable memory. A code object never exists in mutable memory.
|
||||
|
||||
A code object has a zero capacity and is always stone.
|
||||
|
||||
The arity is the maximum number of inputs.
|
||||
|
||||
The size is the capacity of an activation frame that will execute this code.
|
||||
|
||||
The closure size is a reduced capacity for returned frames that survive memory reclamation.
|
||||
|
||||
The entry point is the address at which to begin execution.
|
||||
|
||||
The disruption point is the address of the disruption clause.
|
||||
|
||||
### opaque C objects
|
||||
Records can have opaque C data attached to them.
|
||||
|
||||
A C class can register a GC clean up, and a GC trace function. The trace function is called when the record is encountered in the live object graph; and it should mark any values it wants to keep alive in that function.
|
||||
|
||||
The system maintains an array of live opaque C objects. When such an object is encountered, it marks it as live in the array. When the GC completes, it iterates this array and calls the GC clean up function for each C object in the array with alive=0. Alive is then cleared for the next GC cycle.
|
||||
|
||||
## 32 bit build
|
||||
~3 bit type
|
||||
1 bit stone
|
||||
1 bit memory reclamation flag
|
||||
27 bit capacity
|
||||
|
||||
Key differences here are
|
||||
|
||||
blob max capacity is 2**27 bits = 2**24 bytes = 16 MB [this likely needs addressed]
|
||||
|
||||
fwd is type ...0, and the pointer is 31 bits
|
||||
other types are
|
||||
111 array
|
||||
101 object
|
||||
011 blob
|
||||
001
|
||||
|
||||
## Memory
|
||||
Cell uses a single block of memory that it doles out as needed to the actors in its system.
|
||||
|
||||
Actors are given a block of memory in standard sizes using a doubling buddy memory manager. An actor is given an immutable data section on birth, as well as a mutable data section. When its mutable data becomes full, it requests a new one. Actors utilize their mutable memory with a simple bump allocation. If there is not sufficient memory available, the actor suspends and its status changes to exhausted.
|
||||
|
||||
The smallest block size is determined per platform, but it can be as small as 4KB on 64 bit systems.
|
||||
|
||||
The actor is then given a new block of memory of the same size, and it runs a garbage collector to reclaim memory. It uses the cheney copying algorithm. If a disappointing amount of memory was reclaimed, it is noted, and the actor is given a larger block of memory on the next request.
|
||||
156
docs/nota.md
Normal file
156
docs/nota.md
Normal file
@@ -0,0 +1,156 @@
|
||||
---
|
||||
title: "Nota Format"
|
||||
description: "Network Object Transfer Arrangement"
|
||||
weight: 85
|
||||
type: "docs"
|
||||
---
|
||||
|
||||
Nota is a binary message format developed for use in the Procession Protocol. It provides a compact, JSON-like encoding that supports blobs, text, arrays, records, numbers, and symbols.
|
||||
|
||||
Nota stands for Network Object Transfer Arrangement.
|
||||
|
||||
## Design Philosophy
|
||||
|
||||
JSON had three design rules: minimal, textual, and subset of JavaScript. The textual and JavaScript rules are no longer necessary. Nota maintains JSON's philosophy of being at the intersection of most programming languages and most data types, but departs by using counts instead of brackets and binary encoding instead of text.
|
||||
|
||||
Nota uses Kim continuation bytes for counts and character encoding. See [Kim Encoding](#kim) for details.
|
||||
|
||||
## Type Summary
|
||||
|
||||
| Bits | Type |
|
||||
|------|------|
|
||||
| `000` | Blob |
|
||||
| `001` | Text |
|
||||
| `010` | Array |
|
||||
| `011` | Record |
|
||||
| `100` | Floating Point (positive exponent) |
|
||||
| `101` | Floating Point (negative exponent) |
|
||||
| `110` | Integer (zero exponent) |
|
||||
| `111` | Symbol |
|
||||
|
||||
## Preambles
|
||||
|
||||
Every Nota value starts with a preamble byte that is a Kim value with the three most significant bits used for type information.
|
||||
|
||||
Most types provide 3 or 4 data bits in the preamble. If the Kim encoding of the data fits in those bits, it is incorporated directly and the continue bit is off. Otherwise the continue bit is on and the continuation follows.
|
||||
|
||||
## Blob
|
||||
|
||||
```
|
||||
C 0 0 0 D D D D
|
||||
```
|
||||
|
||||
- **C** — continue the number of bits
|
||||
- **DDDD** — the number of bits
|
||||
|
||||
A blob is a string of bits. The data produces the number of bits. The number of bytes that follow: `floor((number_of_bits + 7) / 8)`. The final byte is padded with 0 if necessary.
|
||||
|
||||
Example: A blob containing 25 bits `1111000011100011001000001`:
|
||||
|
||||
```
|
||||
80 19 F0 E3 20 80
|
||||
```
|
||||
|
||||
## Text
|
||||
|
||||
```
|
||||
C 0 0 1 D D D D
|
||||
```
|
||||
|
||||
- **C** — continue the number of characters
|
||||
- **DDDD** — the number of characters
|
||||
|
||||
The data produces the number of characters. Kim-encoded characters follow. ASCII characters are 1 byte, first quarter BMP characters are 2 bytes, all other Unicode characters are 3 bytes. Unlike JSON, there is never a need for escapement.
|
||||
|
||||
Examples:
|
||||
|
||||
```
|
||||
"" → 10
|
||||
"cat" → 13 63 61 74
|
||||
```
|
||||
|
||||
## Array
|
||||
|
||||
```
|
||||
C 0 1 0 D D D D
|
||||
```
|
||||
|
||||
- **C** — continue the number of elements
|
||||
- **DDDD** — the number of elements
|
||||
|
||||
An array is an ordered sequence of values. Following the preamble are the elements, each beginning with its own preamble. Nesting is encouraged.
|
||||
|
||||
## Record
|
||||
|
||||
```
|
||||
C 0 1 1 D D D D
|
||||
```
|
||||
|
||||
- **C** — continue the number of pairs
|
||||
- **DDDD** — the number of pairs
|
||||
|
||||
A record is an unordered collection of key/value pairs. Keys must be text and must be unique within the record. Values can be any Nota type.
|
||||
|
||||
## Floating Point
|
||||
|
||||
```
|
||||
C 1 0 E S D D D
|
||||
```
|
||||
|
||||
- **C** — continue the exponent
|
||||
- **E** — sign of the exponent
|
||||
- **S** — sign of the coefficient
|
||||
- **DDD** — three bits of the exponent
|
||||
|
||||
Nota floating point represents numbers as `coefficient * 10^exponent`. The coefficient must be an integer. The preamble may contain the first three bits of the exponent, followed by the continuation of the exponent (if any), followed by the coefficient.
|
||||
|
||||
Use the integer type when the exponent is zero.
|
||||
|
||||
Examples:
|
||||
|
||||
```
|
||||
-1.01 → 5A 65
|
||||
98.6 → 51 87 5A
|
||||
-0.5772156649 → D8 0A 95 C0 B0 BD 69
|
||||
-10000000000000 → C8 0D 01
|
||||
```
|
||||
|
||||
## Integer
|
||||
|
||||
```
|
||||
C 1 1 0 S D D D
|
||||
```
|
||||
|
||||
- **C** — continue the integer
|
||||
- **S** — sign
|
||||
- **DDD** — three bits of the integer
|
||||
|
||||
Integers in the range -7 to 7 fit in a single byte. Integers in the range -1023 to 1023 fit in two bytes. Integers in the range -131071 to 131071 fit in three bytes.
|
||||
|
||||
Examples:
|
||||
|
||||
```
|
||||
0 → 60
|
||||
2023 → E0 8F 67
|
||||
-1 → 69
|
||||
```
|
||||
|
||||
## Symbol
|
||||
|
||||
```
|
||||
0 1 1 1 D D D D
|
||||
```
|
||||
|
||||
- **DDDD** — the symbol
|
||||
|
||||
There are currently five symbols:
|
||||
|
||||
```
|
||||
null → 70
|
||||
false → 72
|
||||
true → 73
|
||||
private → 78
|
||||
system → 79
|
||||
```
|
||||
|
||||
The private prefix must be followed by a record containing a private process address. The system prefix must be followed by a record containing a system message. All other symbols are reserved.
|
||||
@@ -1,14 +1,19 @@
|
||||
# Packages
|
||||
---
|
||||
title: "Packages"
|
||||
description: "Code organization and sharing in ƿit"
|
||||
weight: 30
|
||||
type: "docs"
|
||||
---
|
||||
|
||||
Packages are the fundamental unit of code organization and sharing in Cell.
|
||||
Packages are the fundamental unit of code organization and sharing in ƿit.
|
||||
|
||||
## Package Structure
|
||||
|
||||
A package is a directory containing a `cell.toml` manifest:
|
||||
A package is a directory containing a `pit.toml` manifest:
|
||||
|
||||
```
|
||||
mypackage/
|
||||
├── cell.toml # package manifest
|
||||
├── pit.toml # package manifest
|
||||
├── main.ce # entry point (optional)
|
||||
├── utils.cm # module
|
||||
├── helper/
|
||||
@@ -17,7 +22,7 @@ mypackage/
|
||||
└── _internal.cm # private module (underscore prefix)
|
||||
```
|
||||
|
||||
## cell.toml
|
||||
## pit.toml
|
||||
|
||||
The package manifest declares metadata and dependencies:
|
||||
|
||||
@@ -38,11 +43,11 @@ mylib = "/Users/john/work/mylib"
|
||||
|
||||
## Module Resolution
|
||||
|
||||
When importing with `use()`, Cell searches in order:
|
||||
When importing with `use()`, ƿit searches in order:
|
||||
|
||||
1. **Local package** — relative to package root
|
||||
2. **Dependencies** — via aliases in `cell.toml`
|
||||
3. **Core** — built-in Cell modules
|
||||
2. **Dependencies** — via aliases in `pit.toml`
|
||||
3. **Core** — built-in ƿit modules
|
||||
|
||||
```javascript
|
||||
// In package 'myapp' with dependency: renderer = "gitea.pockle.world/john/renderer"
|
||||
@@ -85,10 +90,10 @@ Local packages are symlinked into the shop, making development seamless.
|
||||
|
||||
## The Shop
|
||||
|
||||
Cell stores all packages in the **shop** at `~/.cell/`:
|
||||
ƿit stores all packages in the **shop** at `~/.pit/`:
|
||||
|
||||
```
|
||||
~/.cell/
|
||||
~/.pit/
|
||||
├── packages/
|
||||
│ ├── core -> gitea.pockle.world/john/cell
|
||||
│ ├── gitea.pockle.world/
|
||||
@@ -134,20 +139,20 @@ target = "/Users/john/work/prosperon"
|
||||
|
||||
```bash
|
||||
# Install from remote
|
||||
cell install gitea.pockle.world/john/prosperon
|
||||
pit install gitea.pockle.world/john/prosperon
|
||||
|
||||
# Install from local path
|
||||
cell install /Users/john/work/mylib
|
||||
pit install /Users/john/work/mylib
|
||||
```
|
||||
|
||||
## Updating Packages
|
||||
|
||||
```bash
|
||||
# Update all
|
||||
cell update
|
||||
pit update
|
||||
|
||||
# Update specific package
|
||||
cell update gitea.pockle.world/john/prosperon
|
||||
pit update gitea.pockle.world/john/prosperon
|
||||
```
|
||||
|
||||
## Development Workflow
|
||||
@@ -156,12 +161,12 @@ For active development, link packages locally:
|
||||
|
||||
```bash
|
||||
# Link a package for development
|
||||
cell link add gitea.pockle.world/john/prosperon /Users/john/work/prosperon
|
||||
pit link add gitea.pockle.world/john/prosperon /Users/john/work/prosperon
|
||||
|
||||
# Changes to /Users/john/work/prosperon are immediately visible
|
||||
|
||||
# Remove link when done
|
||||
cell link delete gitea.pockle.world/john/prosperon
|
||||
pit link delete gitea.pockle.world/john/prosperon
|
||||
```
|
||||
|
||||
## C Extensions
|
||||
@@ -170,14 +175,14 @@ C files in a package are compiled into a dynamic library:
|
||||
|
||||
```
|
||||
mypackage/
|
||||
├── cell.toml
|
||||
├── pit.toml
|
||||
├── render.c # compiled to mypackage.dylib
|
||||
└── render.cm # optional Cell wrapper
|
||||
└── render.cm # optional ƿit wrapper
|
||||
```
|
||||
|
||||
The library is named after the package and placed in `~/.cell/lib/`.
|
||||
The library is named after the package and placed in `~/.pit/lib/`.
|
||||
|
||||
See [Writing C Modules](c-modules.md) for details.
|
||||
See [Writing C Modules](/docs/c-modules/) for details.
|
||||
|
||||
## Platform-Specific Files
|
||||
|
||||
@@ -190,4 +195,4 @@ mypackage/
|
||||
└── audio_emscripten.c # Web-specific
|
||||
```
|
||||
|
||||
Cell selects the appropriate file based on the build target.
|
||||
ƿit selects the appropriate file based on the build target.
|
||||
|
||||
176
docs/requestors.md
Normal file
176
docs/requestors.md
Normal file
@@ -0,0 +1,176 @@
|
||||
---
|
||||
title: "Requestors"
|
||||
description: "Asynchronous work with requestors"
|
||||
weight: 25
|
||||
type: "docs"
|
||||
---
|
||||
|
||||
Requestors are functions that encapsulate asynchronous work. They provide a structured way to compose callbacks, manage cancellation, and coordinate concurrent operations between actors.
|
||||
|
||||
## What is a Requestor
|
||||
|
||||
A requestor is a function with this signature:
|
||||
|
||||
```javascript
|
||||
var my_requestor = function(callback, value) {
|
||||
// Do async work, then call callback with result
|
||||
// Return a cancel function
|
||||
}
|
||||
```
|
||||
|
||||
- **callback** — called when the work completes: `callback(value, reason)`
|
||||
- On success: `callback(result)` or `callback(result, null)`
|
||||
- On failure: `callback(null, reason)` where reason explains the failure
|
||||
- **value** — input passed from the previous step (or the initial caller)
|
||||
- **return** — a cancel function, or null if cancellation is not supported
|
||||
|
||||
The cancel function, when called, should abort the in-progress work.
|
||||
|
||||
## Writing a Requestor
|
||||
|
||||
```javascript
|
||||
var fetch_data = function(callback, url) {
|
||||
$contact(function(connection) {
|
||||
$send(connection, {get: url}, function(response) {
|
||||
callback(response)
|
||||
})
|
||||
}, {host: url, port: 80})
|
||||
return function() {
|
||||
// clean up if needed
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
A requestor that always succeeds immediately:
|
||||
|
||||
```javascript
|
||||
var constant = function(callback, value) {
|
||||
callback(42)
|
||||
}
|
||||
```
|
||||
|
||||
A requestor that always fails:
|
||||
|
||||
```javascript
|
||||
var broken = function(callback, value) {
|
||||
callback(null, "something went wrong")
|
||||
}
|
||||
```
|
||||
|
||||
## Composing Requestors
|
||||
|
||||
ƿit provides four built-in functions for composing requestors into pipelines.
|
||||
|
||||
### sequence(requestor_array)
|
||||
|
||||
Run requestors one after another. Each result becomes the input to the next. The final result is passed to the callback.
|
||||
|
||||
```javascript
|
||||
var pipeline = sequence([
|
||||
fetch_user,
|
||||
validate_permissions,
|
||||
load_profile
|
||||
])
|
||||
|
||||
pipeline(function(profile, reason) {
|
||||
if (reason) {
|
||||
print(reason)
|
||||
} else {
|
||||
print(profile.name)
|
||||
}
|
||||
}, user_id)
|
||||
```
|
||||
|
||||
If any step fails, the remaining steps are skipped and the failure propagates.
|
||||
|
||||
### parallel(requestor_array, throttle, need)
|
||||
|
||||
Start all requestors concurrently. Results are collected into an array matching the input order.
|
||||
|
||||
```javascript
|
||||
var both = parallel([
|
||||
fetch_profile,
|
||||
fetch_settings
|
||||
])
|
||||
|
||||
both(function(results, reason) {
|
||||
var profile = results[0]
|
||||
var settings = results[1]
|
||||
}, user_id)
|
||||
```
|
||||
|
||||
- **throttle** — limit how many requestors run at once (null for no limit)
|
||||
- **need** — minimum number of successes required (default: all)
|
||||
|
||||
### race(requestor_array, throttle, need)
|
||||
|
||||
Like `parallel`, but returns as soon as the needed number of results arrive. Unfinished requestors are cancelled.
|
||||
|
||||
```javascript
|
||||
var fastest = race([
|
||||
fetch_from_cache,
|
||||
fetch_from_network,
|
||||
fetch_from_backup
|
||||
])
|
||||
|
||||
fastest(function(results) {
|
||||
// results[0] is whichever responded first
|
||||
}, request)
|
||||
```
|
||||
|
||||
Default need is 1. Useful for redundant operations where only one result matters.
|
||||
|
||||
### fallback(requestor_array)
|
||||
|
||||
Try each requestor in order. If one fails, try the next. Return the first success.
|
||||
|
||||
```javascript
|
||||
var resilient = fallback([
|
||||
fetch_from_primary,
|
||||
fetch_from_secondary,
|
||||
use_cached_value
|
||||
])
|
||||
|
||||
resilient(function(data, reason) {
|
||||
if (reason) {
|
||||
print("all sources failed")
|
||||
}
|
||||
}, key)
|
||||
```
|
||||
|
||||
## Timeouts
|
||||
|
||||
Wrap any requestor with `$time_limit` to add a timeout:
|
||||
|
||||
```javascript
|
||||
var timed = $time_limit(fetch_data, 5) // 5 second timeout
|
||||
|
||||
timed(function(result, reason) {
|
||||
// reason will explain timeout if it fires
|
||||
}, url)
|
||||
```
|
||||
|
||||
If the requestor does not complete within the time limit, it is cancelled and the callback receives a failure.
|
||||
|
||||
## Requestors and Actors
|
||||
|
||||
Requestors are particularly useful with actor messaging. Since `$send` is callback-based, it fits naturally:
|
||||
|
||||
```javascript
|
||||
var ask_worker = function(callback, task) {
|
||||
$send(worker, task, function(reply) {
|
||||
callback(reply)
|
||||
})
|
||||
}
|
||||
|
||||
var pipeline = sequence([
|
||||
ask_worker,
|
||||
process_result,
|
||||
store_result
|
||||
])
|
||||
|
||||
pipeline(function(stored) {
|
||||
print("done")
|
||||
$stop()
|
||||
}, {type: "compute", data: [1, 2, 3]})
|
||||
```
|
||||
296
docs/spec/c-runtime.md
Normal file
296
docs/spec/c-runtime.md
Normal file
@@ -0,0 +1,296 @@
|
||||
---
|
||||
title: "C Runtime for Native Code"
|
||||
description: "Minimum C runtime surface for QBE-generated native code"
|
||||
---
|
||||
|
||||
## Overview
|
||||
|
||||
QBE-generated native code calls into a C runtime for anything that touches the heap, dispatches dynamically, or requires GC awareness. The design principle: **native code handles control flow and integer math directly; everything else is a runtime call.**
|
||||
|
||||
This document defines the runtime boundary — what must be in C, what QBE handles inline, and how to organize the C code to serve both the mcode interpreter and native code cleanly.
|
||||
|
||||
## The Boundary
|
||||
|
||||
### What native code does inline (no C calls)
|
||||
|
||||
These operations compile to straight QBE instructions with no runtime involvement:
|
||||
|
||||
- **Integer arithmetic**: `add`, `sub`, `mul` on NaN-boxed ints (shift right 1, operate, shift left 1)
|
||||
- **Integer comparisons**: extract int with shift, compare, produce tagged bool
|
||||
- **Control flow**: jumps, branches, labels, function entry/exit
|
||||
- **Slot access**: load/store to frame slots via `%fp` + offset
|
||||
- **NaN-box tagging**: integer tagging (`n << 1`), bool constants (`0x03`/`0x23`), null (`0x07`)
|
||||
- **Type tests**: `JS_IsInt` (LSB check), `JS_IsNumber`, `JS_IsText`, `JS_IsNull` — these are bit tests on the value, no heap access needed
|
||||
|
||||
### What requires a C call
|
||||
|
||||
Anything that:
|
||||
1. **Allocates** (arrays, records, strings, frames, function objects)
|
||||
2. **Touches the heap** (property get/set, array indexing, closure access)
|
||||
3. **Dispatches on type at runtime** (dynamic load/store, polymorphic arithmetic)
|
||||
4. **Calls user functions** (frame setup, argument passing, invocation)
|
||||
5. **Does string operations** (concatenation, comparison, conversion)
|
||||
|
||||
## Runtime Functions
|
||||
|
||||
### Tier 1: Essential (must exist for any program to run)
|
||||
|
||||
These are called by virtually every QBE program.
|
||||
|
||||
#### Intrinsic Lookup
|
||||
|
||||
```c
|
||||
// Look up a built-in function by name. Called once per intrinsic per callsite.
|
||||
JSValue cell_rt_get_intrinsic(JSContext *ctx, const char *name);
|
||||
```
|
||||
|
||||
Maps name → C function pointer wrapped in JSValue. This is the primary entry point for all built-in functions (`print`, `text`, `length`, `is_array`, etc). The native code never calls intrinsics directly — it always goes through `get_intrinsic` → `frame` → `invoke`.
|
||||
|
||||
#### Function Calls
|
||||
|
||||
```c
|
||||
// Allocate a call frame with space for nr_args arguments.
|
||||
JSValue cell_rt_frame(JSContext *ctx, JSValue fn, int nr_args);
|
||||
|
||||
// Set argument idx in the frame.
|
||||
void cell_rt_setarg(JSValue frame, int idx, JSValue val);
|
||||
|
||||
// Execute the function. Returns the result.
|
||||
JSValue cell_rt_invoke(JSContext *ctx, JSValue frame);
|
||||
```
|
||||
|
||||
This is the universal calling convention. Every function call — user functions, intrinsics, methods — goes through frame/setarg/invoke. The frame allocates a `JSFrameRegister` on the GC heap, setarg fills slots, invoke dispatches.
|
||||
|
||||
**Tail call variants:**
|
||||
|
||||
```c
|
||||
JSValue cell_rt_goframe(JSContext *ctx, JSValue fn, int nr_args);
|
||||
void cell_rt_goinvoke(JSContext *ctx, JSValue frame);
|
||||
```
|
||||
|
||||
Same as frame/invoke but reuse the caller's stack position.
|
||||
|
||||
### Tier 2: Property Access (needed by any program using records or arrays)
|
||||
|
||||
```c
|
||||
// Record field by constant name.
|
||||
JSValue cell_rt_load_field(JSContext *ctx, JSValue obj, const char *name);
|
||||
void cell_rt_store_field(JSContext *ctx, JSValue obj, JSValue val, const char *name);
|
||||
|
||||
// Array element by integer index.
|
||||
JSValue cell_rt_load_index(JSContext *ctx, JSValue obj, JSValue idx);
|
||||
void cell_rt_store_index(JSContext *ctx, JSValue obj, JSValue idx, JSValue val);
|
||||
|
||||
// Dynamic — type of key unknown at compile time.
|
||||
JSValue cell_rt_load_dynamic(JSContext *ctx, JSValue obj, JSValue key);
|
||||
void cell_rt_store_dynamic(JSContext *ctx, JSValue obj, JSValue key, JSValue val);
|
||||
```
|
||||
|
||||
The typed variants (`load_field`/`load_index`) skip the key-type dispatch that `load_dynamic` must do. When parse and fold provide type information, QBE emit selects the typed variant and the streamline optimizer can narrow dynamic → typed.
|
||||
|
||||
**Implementation**: These are thin wrappers around existing `JS_GetPropertyStr`/`JS_GetPropertyNumber`/`JS_GetProperty` and their `Set` counterparts.
|
||||
|
||||
### Tier 3: Closures (needed by programs with nested functions)
|
||||
|
||||
```c
|
||||
// Walk depth levels up the frame chain, read slot.
|
||||
JSValue cell_rt_get_closure(JSContext *ctx, JSValue fp, int depth, int slot);
|
||||
|
||||
// Walk depth levels up, write slot.
|
||||
void cell_rt_put_closure(JSContext *ctx, JSValue fp, JSValue val, int depth, int slot);
|
||||
```
|
||||
|
||||
Closure variables live in outer frames. `depth` is how many `caller` links to follow; `slot` is the register index in that frame.
|
||||
|
||||
### Tier 4: Object Construction (needed by programs creating arrays/records/functions)
|
||||
|
||||
```c
|
||||
// Create a function object from a compiled function index.
|
||||
// The native code loader must maintain a function table.
|
||||
JSValue cell_rt_make_function(JSContext *ctx, int fn_id);
|
||||
```
|
||||
|
||||
Array and record literals are currently compiled as intrinsic calls (`array(...)`, direct `{...}` construction) which go through the frame/invoke path. A future optimization could add:
|
||||
|
||||
```c
|
||||
// Fast paths (optional, not yet needed)
|
||||
JSValue cell_rt_new_array(JSContext *ctx, int len);
|
||||
JSValue cell_rt_new_record(JSContext *ctx);
|
||||
```
|
||||
|
||||
### Tier 5: Collection Operations
|
||||
|
||||
```c
|
||||
// a[] = val (push) and var v = a[] (pop)
|
||||
void cell_rt_push(JSContext *ctx, JSValue arr, JSValue val);
|
||||
JSValue cell_rt_pop(JSContext *ctx, JSValue arr);
|
||||
```
|
||||
|
||||
### Tier 6: Error Handling
|
||||
|
||||
```c
|
||||
// Trigger disruption. Jumps to the disrupt handler or unwinds.
|
||||
void cell_rt_disrupt(JSContext *ctx);
|
||||
```
|
||||
|
||||
### Tier 7: Miscellaneous
|
||||
|
||||
```c
|
||||
JSValue cell_rt_delete(JSContext *ctx, JSValue obj, JSValue key);
|
||||
JSValue cell_rt_typeof(JSContext *ctx, JSValue val);
|
||||
```
|
||||
|
||||
### Tier 8: String and Float Helpers (called from QBE inline code, not from qbe_emit)
|
||||
|
||||
These are called from the QBE IL that `qbe.cm` generates inline for arithmetic and comparison operations. They're not `cell_rt_` prefixed — they're lower-level:
|
||||
|
||||
```c
|
||||
// Float arithmetic (when operands aren't both ints)
|
||||
JSValue qbe_float_add(JSContext *ctx, JSValue a, JSValue b);
|
||||
JSValue qbe_float_sub(JSContext *ctx, JSValue a, JSValue b);
|
||||
JSValue qbe_float_mul(JSContext *ctx, JSValue a, JSValue b);
|
||||
JSValue qbe_float_div(JSContext *ctx, JSValue a, JSValue b);
|
||||
JSValue qbe_float_mod(JSContext *ctx, JSValue a, JSValue b);
|
||||
JSValue qbe_float_pow(JSContext *ctx, JSValue a, JSValue b);
|
||||
JSValue qbe_float_neg(JSContext *ctx, JSValue v);
|
||||
JSValue qbe_float_inc(JSContext *ctx, JSValue v);
|
||||
JSValue qbe_float_dec(JSContext *ctx, JSValue v);
|
||||
|
||||
// Float comparison (returns C int 0/1 for QBE branching)
|
||||
int qbe_float_cmp(JSContext *ctx, int op, JSValue a, JSValue b);
|
||||
|
||||
// Bitwise ops on non-int values (convert to int32 first)
|
||||
JSValue qbe_bnot(JSContext *ctx, JSValue v);
|
||||
JSValue qbe_bitwise_and(JSContext *ctx, JSValue a, JSValue b);
|
||||
JSValue qbe_bitwise_or(JSContext *ctx, JSValue a, JSValue b);
|
||||
JSValue qbe_bitwise_xor(JSContext *ctx, JSValue a, JSValue b);
|
||||
JSValue qbe_shift_shl(JSContext *ctx, JSValue a, JSValue b);
|
||||
JSValue qbe_shift_sar(JSContext *ctx, JSValue a, JSValue b);
|
||||
JSValue qbe_shift_shr(JSContext *ctx, JSValue a, JSValue b);
|
||||
|
||||
// String operations
|
||||
JSValue JS_ConcatString(JSContext *ctx, JSValue a, JSValue b);
|
||||
int js_string_compare_value(JSContext *ctx, JSValue a, JSValue b, int eq_only);
|
||||
JSValue JS_NewString(JSContext *ctx, const char *str);
|
||||
JSValue __JS_NewFloat64(JSContext *ctx, double d);
|
||||
int JS_ToBool(JSContext *ctx, JSValue v);
|
||||
|
||||
// String/number type tests (inline-able but currently calls)
|
||||
int JS_IsText(JSValue v);
|
||||
int JS_IsNumber(JSValue v);
|
||||
|
||||
// Tolerant equality (== on mixed types)
|
||||
JSValue cell_rt_eq_tol(JSContext *ctx, JSValue a, JSValue b);
|
||||
JSValue cell_rt_ne_tol(JSContext *ctx, JSValue a, JSValue b);
|
||||
|
||||
// Text ordering comparisons
|
||||
JSValue cell_rt_lt_text(JSContext *ctx, JSValue a, JSValue b);
|
||||
JSValue cell_rt_le_text(JSContext *ctx, JSValue a, JSValue b);
|
||||
JSValue cell_rt_gt_text(JSContext *ctx, JSValue a, JSValue b);
|
||||
JSValue cell_rt_ge_text(JSContext *ctx, JSValue a, JSValue b);
|
||||
```
|
||||
|
||||
## What Exists vs What Needs Writing
|
||||
|
||||
### Already exists (in qbe_helpers.c)
|
||||
|
||||
All `qbe_float_*`, `qbe_bnot`, `qbe_bitwise_*`, `qbe_shift_*`, `qbe_to_bool` — these are implemented and working.
|
||||
|
||||
### Already exists (in runtime.c / quickjs.c) but not yet wrapped
|
||||
|
||||
The underlying operations exist but aren't exposed with the `cell_rt_` names:
|
||||
|
||||
| Runtime function | Underlying implementation |
|
||||
|---|---|
|
||||
| `cell_rt_load_field` | `JS_GetPropertyStr(ctx, obj, name)` |
|
||||
| `cell_rt_load_index` | `JS_GetPropertyNumber(ctx, obj, JS_VALUE_GET_INT(idx))` |
|
||||
| `cell_rt_load_dynamic` | `JS_GetProperty(ctx, obj, key)` |
|
||||
| `cell_rt_store_field` | `JS_SetPropertyStr(ctx, obj, name, val)` |
|
||||
| `cell_rt_store_index` | `JS_SetPropertyNumber(ctx, obj, JS_VALUE_GET_INT(idx), val)` |
|
||||
| `cell_rt_store_dynamic` | `JS_SetProperty(ctx, obj, key, val)` |
|
||||
| `cell_rt_delete` | `JS_DeleteProperty(ctx, obj, key)` |
|
||||
| `cell_rt_push` | `JS_ArrayPush(ctx, &arr, val)` |
|
||||
| `cell_rt_pop` | `JS_ArrayPop(ctx, arr)` |
|
||||
| `cell_rt_typeof` | type tag switch → `JS_NewString` |
|
||||
| `cell_rt_disrupt` | `JS_Throw(ctx, ...)` |
|
||||
| `cell_rt_eq_tol` / `cell_rt_ne_tol` | comparison logic in mcode.c `eq_tol`/`ne_tol` handler |
|
||||
| `cell_rt_lt_text` etc. | `js_string_compare_value` + wrap result |
|
||||
|
||||
### Needs new code
|
||||
|
||||
| Runtime function | What's needed |
|
||||
|---|---|
|
||||
| `cell_rt_get_intrinsic` | Look up intrinsic by name string, return JSValue function. Currently scattered across `js_cell_intrinsic_get` and the mcode handler. Needs a clean single entry point. |
|
||||
| `cell_rt_frame` | Allocate `JSFrameRegister`, set function slot, set argc. Exists in mcode.c `frame` handler but not as a callable function. |
|
||||
| `cell_rt_setarg` | Write to frame slot. Trivial: `frame->slots[idx + 1] = val` (slot 0 is `this`). |
|
||||
| `cell_rt_invoke` | Call the function in the frame. Needs to dispatch: native C function vs mach bytecode vs mcode. This is the critical piece — it must handle all function types. |
|
||||
| `cell_rt_goframe` / `cell_rt_goinvoke` | Tail call variants. Similar to frame/invoke but reuse caller frame. |
|
||||
| `cell_rt_make_function` | Create function object from index. Needs a function table (populated by the native loader). |
|
||||
| `cell_rt_get_closure` / `cell_rt_put_closure` | Walk frame chain. Exists inline in mcode.c `get`/`put` handlers. |
|
||||
|
||||
## Recommended C File Organization
|
||||
|
||||
```
|
||||
source/
|
||||
cell_runtime.c — NEW: all cell_rt_* functions (the native code API)
|
||||
qbe_helpers.c — existing: float/bitwise/shift helpers for inline QBE
|
||||
runtime.c — existing: JS_GetProperty, JS_SetProperty, etc.
|
||||
quickjs.c — existing: core VM, GC, value representation
|
||||
mcode.c — existing: mcode interpreter (can delegate to cell_runtime.c)
|
||||
```
|
||||
|
||||
**`cell_runtime.c`** is the single file that defines the native code contract. It should:
|
||||
|
||||
1. Include `quickjs-internal.h` for access to value representation and heap types
|
||||
2. Export all `cell_rt_*` functions with C linkage (no `static`)
|
||||
3. Keep each function thin — delegate to existing `JS_*` functions where possible
|
||||
4. Handle GC safety: after any allocation (frame, string, array), callers' frames may have moved
|
||||
|
||||
### Implementation Priority
|
||||
|
||||
**Phase 1** — Get "hello world" running natively:
|
||||
- `cell_rt_get_intrinsic` (to find `print` and `text`)
|
||||
- `cell_rt_frame`, `cell_rt_setarg`, `cell_rt_invoke` (to call them)
|
||||
- A loader that takes QBE output → assembles → links → calls `cell_main`
|
||||
|
||||
**Phase 2** — Variables and arithmetic:
|
||||
- All property access (`load_field`, `load_index`, `store_*`, `load_dynamic`)
|
||||
- `cell_rt_make_function`, `cell_rt_get_closure`, `cell_rt_put_closure`
|
||||
|
||||
**Phase 3** — Full language:
|
||||
- `cell_rt_push`, `cell_rt_pop`, `cell_rt_delete`, `cell_rt_typeof`
|
||||
- `cell_rt_disrupt`
|
||||
- `cell_rt_goframe`, `cell_rt_goinvoke`
|
||||
- Text comparison wrappers (`cell_rt_lt_text`, etc.)
|
||||
- Tolerant equality (`cell_rt_eq_tol`, `cell_rt_ne_tol`)
|
||||
|
||||
## Calling Convention
|
||||
|
||||
All `cell_rt_*` functions follow the same pattern:
|
||||
|
||||
- First argument is always `JSContext *ctx`
|
||||
- Values are passed/returned as `JSValue` (64-bit, by value)
|
||||
- Frame pointers are `JSValue` (tagged pointer to `JSFrameRegister`)
|
||||
- String names are `const char *` (pointer to data section label)
|
||||
- Integer constants (slot indices, arg counts) are `int` / `long`
|
||||
|
||||
Native code maintains `%ctx` (JSContext) and `%fp` (current frame pointer) as persistent values across the function body. All slot reads/writes go through `%fp` + offset.
|
||||
|
||||
## What Should NOT Be in the C Runtime
|
||||
|
||||
These are handled entirely by QBE-generated code:
|
||||
|
||||
- **Integer arithmetic and comparisons** — bit operations on NaN-boxed values
|
||||
- **Control flow** — branches, loops, labels, jumps
|
||||
- **Boolean logic** — `and`/`or`/`not` on tagged values
|
||||
- **Constant loading** — integer constants are immediate, strings are data labels
|
||||
- **Type guard branches** — the `is_int`/`is_text`/`is_null` checks are inline bit tests; the branch to the float or text path is just a QBE `jnz`
|
||||
|
||||
The `qbe.cm` macros already handle all of this. The arithmetic path looks like:
|
||||
|
||||
```
|
||||
check both ints? → yes → inline int add → done
|
||||
→ no → call qbe_float_add (or JS_ConcatString for text)
|
||||
```
|
||||
|
||||
The C runtime is only called on the slow paths (float, text, dynamic dispatch). The fast path (integer arithmetic, comparisons, branching) is fully native.
|
||||
77
docs/spec/dec64.md
Normal file
77
docs/spec/dec64.md
Normal file
@@ -0,0 +1,77 @@
|
||||
---
|
||||
title: "DEC64 Numbers"
|
||||
description: "Decimal floating point representation"
|
||||
---
|
||||
|
||||
## Overview
|
||||
|
||||
ƿit uses DEC64 as its number format. DEC64 represents numbers as `coefficient * 10^exponent` in a 64-bit word. This eliminates the rounding errors that plague IEEE 754 binary floating point — `0.1 + 0.2` is exactly `0.3`.
|
||||
|
||||
DEC64 was designed by Douglas Crockford as a general-purpose number type suitable for both business and scientific computation.
|
||||
|
||||
## Format
|
||||
|
||||
A DEC64 number is a 64-bit value:
|
||||
|
||||
```
|
||||
[coefficient: 56 bits][exponent: 8 bits]
|
||||
```
|
||||
|
||||
- **Coefficient** — a 56-bit signed integer (two's complement)
|
||||
- **Exponent** — an 8-bit signed integer (range: -127 to 127)
|
||||
|
||||
The value of a DEC64 number is: `coefficient * 10^exponent`
|
||||
|
||||
### Examples
|
||||
|
||||
| Value | Coefficient | Exponent | Hex |
|
||||
|-------|------------|----------|-----|
|
||||
| `0` | 0 | 0 | `0000000000000000` |
|
||||
| `1` | 1 | 0 | `0000000000000100` |
|
||||
| `3.14159` | 314159 | -5 | `000000004CB2FFFB` |
|
||||
| `-1` | -1 | 0 | `FFFFFFFFFFFFFF00` |
|
||||
| `1000000` | 1 | 6 | `0000000000000106` |
|
||||
|
||||
## Special Values
|
||||
|
||||
### Null
|
||||
|
||||
The exponent `0x80` (-128) indicates null. This is the only special value — there is no infinity, no NaN, no negative zero. Operations that would produce undefined results (such as division by zero) return null.
|
||||
|
||||
```
|
||||
coefficient: any, exponent: 0x80 → null
|
||||
```
|
||||
|
||||
## Arithmetic Properties
|
||||
|
||||
- **Exact decimals**: All decimal fractions with up to 17 significant digits are represented exactly
|
||||
- **No rounding**: `0.1 + 0.2 == 0.3` is true
|
||||
- **Integer range**: Exact integers up to 2^55 (about 3.6 * 10^16)
|
||||
- **Normalized on demand**: The runtime normalizes coefficients to remove trailing zeros when needed for comparison
|
||||
|
||||
## Comparison with IEEE 754
|
||||
|
||||
| Property | DEC64 | IEEE 754 double |
|
||||
|----------|-------|----------------|
|
||||
| Decimal fractions | Exact | Approximate |
|
||||
| Significant digits | ~17 | ~15-16 |
|
||||
| Special values | null only | NaN, ±Infinity, -0 |
|
||||
| Rounding errors | None (decimal) | Common |
|
||||
| Financial arithmetic | Correct | Requires libraries |
|
||||
| Scientific range | ±10^127 | ±10^308 |
|
||||
|
||||
DEC64 trades a smaller exponent range for exact decimal arithmetic. Most applications never need exponents beyond ±127.
|
||||
|
||||
## In ƿit
|
||||
|
||||
All numbers in ƿit are DEC64. There is no separate integer type at the language level — the distinction is internal. The `is_integer` function checks whether a number has no fractional part.
|
||||
|
||||
```javascript
|
||||
var x = 42 // coefficient: 42, exponent: 0
|
||||
var y = 3.14 // coefficient: 314, exponent: -2
|
||||
var z = 1000000 // coefficient: 1, exponent: 6 (normalized)
|
||||
|
||||
is_integer(x) // true
|
||||
is_integer(y) // false
|
||||
1 / 0 // null
|
||||
```
|
||||
82
docs/spec/gc.md
Normal file
82
docs/spec/gc.md
Normal file
@@ -0,0 +1,82 @@
|
||||
---
|
||||
title: "Garbage Collection"
|
||||
description: "Cheney copying collector"
|
||||
---
|
||||
|
||||
## Overview
|
||||
|
||||
ƿit uses a Cheney copying collector for automatic memory management. Each actor has its own independent heap — actors never share mutable memory, so garbage collection is per-actor with no global pauses.
|
||||
|
||||
## Algorithm
|
||||
|
||||
The Cheney algorithm is a two-space copying collector:
|
||||
|
||||
1. **Allocate new space** — a fresh memory block for the new heap
|
||||
2. **Copy roots** — copy all live root objects from old space to new space
|
||||
3. **Scan** — walk the new space, updating all internal references
|
||||
4. **Free old space** — the entire old heap is freed at once
|
||||
|
||||
### Copying and Forwarding
|
||||
|
||||
When an object is copied from old space to new space:
|
||||
|
||||
1. The object's data is copied to the next free position in new space
|
||||
2. The old object's header is overwritten with a **forwarding pointer** (`OBJ_FORWARD`) containing the new address
|
||||
3. Future references to the old address find the forwarding pointer and follow it to the new location
|
||||
|
||||
```
|
||||
Old space: New space:
|
||||
┌──────────────┐ ┌──────────────┐
|
||||
│ OBJ_FORWARD ─┼────────> │ copied object│
|
||||
│ (new addr) │ │ │
|
||||
└──────────────┘ └──────────────┘
|
||||
```
|
||||
|
||||
### Scan Phase
|
||||
|
||||
After roots are copied, the collector scans new space linearly. For each object, it examines every JSValue field:
|
||||
|
||||
- If the field points to old space, copy the referenced object (or follow its forwarding pointer if already copied)
|
||||
- If the field points to stone memory, skip it (stone objects are permanent)
|
||||
- If the field is an immediate value (integer, boolean, null, immediate string), skip it
|
||||
|
||||
The scan continues until the scan pointer catches up with the allocation pointer — at that point, all live objects have been found and copied.
|
||||
|
||||
## Roots
|
||||
|
||||
The collector traces from these root sources:
|
||||
|
||||
- **Global object** — all global variables
|
||||
- **Class prototypes** — built-in type prototypes
|
||||
- **Exception** — the current exception value
|
||||
- **Value stack** — all values on the operand stack
|
||||
- **Frame stack** — all stack frames (register VM and mcode)
|
||||
- **GC reference stack** — manually registered roots (via `JS_PUSH_VALUE` / `JS_POP_VALUE`)
|
||||
- **Parser constant pool** — during compilation, constants being built
|
||||
|
||||
## Per-Actor Heaps
|
||||
|
||||
Each actor maintains its own heap with independent collection:
|
||||
|
||||
- No stop-the-world pauses across actors
|
||||
- No synchronization between collectors
|
||||
- Each actor's GC runs at the end of a turn (between message deliveries)
|
||||
- Heap sizes adapt independently based on each actor's allocation patterns
|
||||
|
||||
## Heap Growth
|
||||
|
||||
The collector uses a buddy allocator for heap blocks. After each collection, if less than 20% of the heap was recovered, the next block size is doubled. The new space size is: `max(live_estimate + alloc_size, next_block_size)`.
|
||||
|
||||
All allocations within a heap block use bump allocation (advance a pointer), which is extremely fast.
|
||||
|
||||
## Alignment
|
||||
|
||||
All objects are aligned to 8-byte boundaries. Object sizes are rounded up to ensure this alignment, which guarantees that the low 3 bits of any heap pointer are always zero — available for JSValue tag bits.
|
||||
|
||||
## Interaction with Stone Memory
|
||||
|
||||
Stone memory objects (S bit set) are never copied by the collector. When the scanner encounters a pointer to stone memory, it leaves it unchanged. This means:
|
||||
|
||||
- Stone objects are effectively permanent GC roots
|
||||
- No overhead for tracing through immutable object graphs
|
||||
- Module return values and interned strings impose zero GC cost
|
||||
165
docs/spec/mach.md
Normal file
165
docs/spec/mach.md
Normal file
@@ -0,0 +1,165 @@
|
||||
---
|
||||
title: "Register VM"
|
||||
description: "Register-based virtual machine (Mach)"
|
||||
---
|
||||
|
||||
## Overview
|
||||
|
||||
The Mach VM is a register-based virtual machine using 32-bit instructions. It is modeled after Lua's register VM — operands are register indices rather than stack positions, reducing instruction count and improving performance.
|
||||
|
||||
## Instruction Formats
|
||||
|
||||
All instructions are 32 bits wide. Four encoding formats are used:
|
||||
|
||||
### iABC — Three-Register
|
||||
|
||||
```
|
||||
[op: 8][A: 8][B: 8][C: 8]
|
||||
```
|
||||
|
||||
Used for operations on three registers: `R(A) = R(B) op R(C)`.
|
||||
|
||||
### iABx — Register + Constant
|
||||
|
||||
```
|
||||
[op: 8][A: 8][Bx: 16]
|
||||
```
|
||||
|
||||
Used for loading constants: `R(A) = K(Bx)`.
|
||||
|
||||
### iAsBx — Register + Signed Offset
|
||||
|
||||
```
|
||||
[op: 8][A: 8][sBx: 16]
|
||||
```
|
||||
|
||||
Used for conditional jumps: if `R(A)` then jump by `sBx`.
|
||||
|
||||
### isJ — Signed Jump
|
||||
|
||||
```
|
||||
[op: 8][sJ: 24]
|
||||
```
|
||||
|
||||
Used for unconditional jumps with a 24-bit signed offset.
|
||||
|
||||
## Registers
|
||||
|
||||
Each function frame has a fixed number of register slots, determined at compile time. Registers hold:
|
||||
|
||||
- **R(0)** — `this` binding
|
||||
- **R(1)..R(arity)** — function arguments
|
||||
- **R(arity+1)..** — local variables and temporaries
|
||||
|
||||
## Instruction Set
|
||||
|
||||
### Loading
|
||||
|
||||
| Opcode | Format | Description |
|
||||
|--------|--------|-------------|
|
||||
| `LOADK` | iABx | `R(A) = K(Bx)` — load from constant pool |
|
||||
| `LOADI` | iAsBx | `R(A) = sBx` — load small integer |
|
||||
| `LOADNULL` | iA | `R(A) = null` |
|
||||
| `LOADTRUE` | iA | `R(A) = true` |
|
||||
| `LOADFALSE` | iA | `R(A) = false` |
|
||||
| `MOVE` | iABC | `R(A) = R(B)` — register copy |
|
||||
|
||||
### Arithmetic
|
||||
|
||||
| Opcode | Format | Description |
|
||||
|--------|--------|-------------|
|
||||
| `ADD` | iABC | `R(A) = R(B) + R(C)` |
|
||||
| `SUB` | iABC | `R(A) = R(B) - R(C)` |
|
||||
| `MUL` | iABC | `R(A) = R(B) * R(C)` |
|
||||
| `DIV` | iABC | `R(A) = R(B) / R(C)` |
|
||||
| `MOD` | iABC | `R(A) = R(B) % R(C)` |
|
||||
| `POW` | iABC | `R(A) = R(B) ^ R(C)` |
|
||||
| `NEG` | iABC | `R(A) = -R(B)` |
|
||||
| `INC` | iABC | `R(A) = R(B) + 1` |
|
||||
| `DEC` | iABC | `R(A) = R(B) - 1` |
|
||||
|
||||
### Comparison
|
||||
|
||||
| Opcode | Format | Description |
|
||||
|--------|--------|-------------|
|
||||
| `EQ` | iABC | `R(A) = R(B) == R(C)` |
|
||||
| `NEQ` | iABC | `R(A) = R(B) != R(C)` |
|
||||
| `LT` | iABC | `R(A) = R(B) < R(C)` |
|
||||
| `LE` | iABC | `R(A) = R(B) <= R(C)` |
|
||||
| `GT` | iABC | `R(A) = R(B) > R(C)` |
|
||||
| `GE` | iABC | `R(A) = R(B) >= R(C)` |
|
||||
|
||||
### Property Access
|
||||
|
||||
| Opcode | Format | Description |
|
||||
|--------|--------|-------------|
|
||||
| `GETFIELD` | iABC | `R(A) = R(B)[K(C)]` — named property |
|
||||
| `SETFIELD` | iABC | `R(A)[K(B)] = R(C)` — set named property |
|
||||
| `GETINDEX` | iABC | `R(A) = R(B)[R(C)]` — computed property |
|
||||
| `SETINDEX` | iABC | `R(A)[R(B)] = R(C)` — set computed property |
|
||||
|
||||
### Variable Resolution
|
||||
|
||||
| Opcode | Format | Description |
|
||||
|--------|--------|-------------|
|
||||
| `GETNAME` | iABx | Unresolved variable (compiler placeholder) |
|
||||
| `GETINTRINSIC` | iABx | Global intrinsic / built-in |
|
||||
| `GETENV` | iABx | Module environment variable |
|
||||
| `GETUP` | iABC | `R(A) = UpFrame(B).slots[C]` — closure upvalue |
|
||||
| `SETUP` | iABC | `UpFrame(A).slots[B] = R(C)` — set closure upvalue |
|
||||
|
||||
### Control Flow
|
||||
|
||||
| Opcode | Format | Description |
|
||||
|--------|--------|-------------|
|
||||
| `JMP` | isJ | Unconditional jump |
|
||||
| `JMPTRUE` | iAsBx | Jump if `R(A)` is true |
|
||||
| `JMPFALSE` | iAsBx | Jump if `R(A)` is false |
|
||||
| `JMPNULL` | iAsBx | Jump if `R(A)` is null |
|
||||
|
||||
### Function Calls
|
||||
|
||||
| Opcode | Format | Description |
|
||||
|--------|--------|-------------|
|
||||
| `CALL` | iABC | Call `R(A)` with `B` args starting at `R(A+1)`, `C`=keep result |
|
||||
| `RETURN` | iA | Return `R(A)` |
|
||||
| `RETNIL` | — | Return null |
|
||||
| `CLOSURE` | iABx | Create closure from function pool entry `Bx` |
|
||||
|
||||
### Object / Array
|
||||
|
||||
| Opcode | Format | Description |
|
||||
|--------|--------|-------------|
|
||||
| `NEWOBJECT` | iA | `R(A) = {}` |
|
||||
| `NEWARRAY` | iABC | `R(A) = array(B)` |
|
||||
| `PUSH` | iABC | Push `R(B)` to array `R(A)` |
|
||||
|
||||
## JSCodeRegister
|
||||
|
||||
The compiled output for a function:
|
||||
|
||||
```c
|
||||
struct JSCodeRegister {
|
||||
uint16_t arity; // argument count
|
||||
uint16_t nr_slots; // total register count
|
||||
uint32_t cpool_count; // constant pool size
|
||||
JSValue *cpool; // constant pool
|
||||
uint32_t instr_count; // instruction count
|
||||
MachInstr32 *instructions; // 32-bit instruction array
|
||||
uint32_t func_count; // nested function count
|
||||
JSCodeRegister **functions; // nested function table
|
||||
JSValue name; // function name
|
||||
uint16_t disruption_pc; // exception handler offset
|
||||
};
|
||||
```
|
||||
|
||||
The constant pool holds all non-immediate values referenced by `LOADK` instructions: strings, large numbers, and other constants.
|
||||
|
||||
### Constant Pool Index Overflow
|
||||
|
||||
Named property instructions (`LOAD_FIELD`, `STORE_FIELD`, `DELETE`) use the iABC format where the constant pool key index occupies an 8-bit field (max 255). When a function references more than 256 unique property names, the serializer automatically falls back to a two-instruction sequence:
|
||||
|
||||
1. `LOADK tmp, key_index` — load the key string into a temporary register (iABx, 16-bit index)
|
||||
2. `LOAD_DYNAMIC` / `STORE_DYNAMIC` / `DELETEINDEX` — use the register-based variant
|
||||
|
||||
This is transparent to the mcode compiler and streamline optimizer.
|
||||
143
docs/spec/mcode.md
Normal file
143
docs/spec/mcode.md
Normal file
@@ -0,0 +1,143 @@
|
||||
---
|
||||
title: "Mcode IR"
|
||||
description: "JSON-based intermediate representation"
|
||||
---
|
||||
|
||||
## Overview
|
||||
|
||||
Mcode is a JSON-based intermediate representation that can be interpreted directly. It represents the same operations as the Mach register VM but uses string-based instruction dispatch rather than binary opcodes. Mcode is intended as an intermediate step toward native code compilation.
|
||||
|
||||
## Pipeline
|
||||
|
||||
```
|
||||
Source → Tokenize → Parse (AST) → Fold → Mcode (JSON) → Streamline → Mach VM (default)
|
||||
→ Mcode Interpreter
|
||||
→ QBE → Native
|
||||
```
|
||||
|
||||
Mcode is produced by `mcode.cm`, which lowers the folded AST to JSON instruction arrays. The streamline optimizer (`streamline.cm`) then eliminates redundant operations. The result is serialized to binary bytecode by the Mach compiler (`mach.c`), interpreted directly by `mcode.c`, or lowered to QBE IL by `qbe_emit.cm` for native compilation. See [Compilation Pipeline](pipeline.md) for the full overview.
|
||||
|
||||
### Function Proxy Decomposition
|
||||
|
||||
When the compiler encounters a method call `obj.method(args)`, it emits a branching pattern to handle ƿit's function proxy protocol. An arity-2 function used as a proxy target receives the method name and argument array instead of a normal method call:
|
||||
|
||||
```json
|
||||
["is_proxy", check, obj]
|
||||
["jump_false", check, "record_path"]
|
||||
|
||||
// Proxy path: call obj(name, [args...]) with this=null
|
||||
["access", name_slot, "method"]
|
||||
["array", args_arr, N, arg0, arg1, ...]
|
||||
["null", null_slot]
|
||||
["frame", f, obj, 2]
|
||||
["setarg", f, 0, null_slot]
|
||||
["setarg", f, 1, name_slot]
|
||||
["setarg", f, 2, args_arr]
|
||||
["invoke", f, dest]
|
||||
["jump", "done"]
|
||||
|
||||
["LABEL", "record_path"]
|
||||
["load_field", method, obj, "method"]
|
||||
["frame", f2, method, N]
|
||||
["setarg", f2, 0, obj]
|
||||
["setarg", f2, 1, arg0]
|
||||
...
|
||||
["invoke", f2, dest]
|
||||
|
||||
["LABEL", "done"]
|
||||
```
|
||||
|
||||
The streamline optimizer can eliminate the dead branch when the type of `obj` is statically known.
|
||||
|
||||
## JSMCode Structure
|
||||
|
||||
```c
|
||||
struct JSMCode {
|
||||
uint16_t nr_args; // argument count
|
||||
uint16_t nr_slots; // register count
|
||||
cJSON **instrs; // pre-flattened instruction array
|
||||
uint32_t instr_count; // number of instructions
|
||||
|
||||
struct {
|
||||
const char *name; // label name
|
||||
uint32_t index; // instruction index
|
||||
} *labels;
|
||||
uint32_t label_count;
|
||||
|
||||
struct JSMCode **functions; // nested functions
|
||||
uint32_t func_count;
|
||||
|
||||
cJSON *json_root; // keeps JSON alive
|
||||
const char *name; // function name
|
||||
const char *filename; // source file
|
||||
uint16_t disruption_pc; // exception handler offset
|
||||
};
|
||||
```
|
||||
|
||||
## Instruction Format
|
||||
|
||||
Each instruction is a JSON array. The first element is the instruction name (string), followed by operands (typically `[op, dest, ...args, line, col]`):
|
||||
|
||||
```json
|
||||
["access", 3, 5, 1, 9]
|
||||
["load_index", 10, 4, 9, 5, 11]
|
||||
["store_dynamic", 4, 11, 12, 6, 3]
|
||||
["frame", 15, 14, 1, 7, 7]
|
||||
["setarg", 15, 0, 16, 7, 7]
|
||||
["invoke", 15, 13, 7, 7]
|
||||
```
|
||||
|
||||
### Typed Load/Store
|
||||
|
||||
Memory operations come in typed variants for optimization:
|
||||
|
||||
- `load_index dest, obj, idx` — array element by integer index
|
||||
- `load_field dest, obj, key` — record property by string key
|
||||
- `load_dynamic dest, obj, key` — unknown; dispatches at runtime
|
||||
- `store_index obj, val, idx` — array element store
|
||||
- `store_field obj, val, key` — record property store
|
||||
- `store_dynamic obj, val, key` — unknown; dispatches at runtime
|
||||
|
||||
The compiler selects the appropriate variant based on `type_tag` and `access_kind` annotations from parse and fold.
|
||||
|
||||
### Decomposed Calls
|
||||
|
||||
Function calls are split into separate instructions:
|
||||
|
||||
- `frame dest, fn, argc` — allocate call frame
|
||||
- `setarg frame, idx, val` — set argument
|
||||
- `invoke frame, result` — execute the call
|
||||
|
||||
## Labels
|
||||
|
||||
Control flow uses named labels instead of numeric offsets:
|
||||
|
||||
```json
|
||||
["LABEL", "loop_start"]
|
||||
["ADD", 1, 1, 2]
|
||||
["JMPFALSE", 3, "loop_end"]
|
||||
["JMP", "loop_start"]
|
||||
["LABEL", "loop_end"]
|
||||
```
|
||||
|
||||
Labels are collected into a name-to-index map during loading, enabling O(1) jump resolution.
|
||||
|
||||
## Differences from Mach
|
||||
|
||||
| Property | Mcode | Mach |
|
||||
|----------|-------|------|
|
||||
| Instructions | cJSON arrays | 32-bit binary |
|
||||
| Dispatch | String comparison | Switch on opcode byte |
|
||||
| Constants | Inline in JSON | Separate constant pool |
|
||||
| Jump targets | Named labels | Numeric offsets |
|
||||
| Memory | Heap (cJSON nodes) | Off-heap (malloc) |
|
||||
|
||||
## Purpose
|
||||
|
||||
Mcode serves as an inspectable, debuggable intermediate format:
|
||||
|
||||
- **Human-readable** — the JSON representation can be printed and examined
|
||||
- **Language-independent** — any tool that produces the correct JSON can target the ƿit runtime
|
||||
- **Compilation target** — the Mach compiler can consume mcode as input, and future native code generators can work from the same representation
|
||||
|
||||
The cost of string-based dispatch makes mcode slower than the binary Mach VM, so it is primarily useful during development and as a compilation intermediate rather than for production execution.
|
||||
141
docs/spec/objects.md
Normal file
141
docs/spec/objects.md
Normal file
@@ -0,0 +1,141 @@
|
||||
---
|
||||
title: "Object Types"
|
||||
description: "Heap object header format and types"
|
||||
---
|
||||
|
||||
## Object Header
|
||||
|
||||
Every heap-allocated object begins with a 64-bit header word (`objhdr_t`):
|
||||
|
||||
```
|
||||
[capacity: 56 bits][flags: 5 bits][type: 3 bits]
|
||||
```
|
||||
|
||||
### Type Field (bits 0-2)
|
||||
|
||||
| Value | Type | Description |
|
||||
|-------|------|-------------|
|
||||
| 0 | `OBJ_ARRAY` | Dynamic array of JSValues |
|
||||
| 1 | `OBJ_BLOB` | Binary data (bits) |
|
||||
| 2 | `OBJ_TEXT` | Unicode text string |
|
||||
| 3 | `OBJ_RECORD` | Key-value object with prototype chain |
|
||||
| 4 | `OBJ_FUNCTION` | Function (C, register, or mcode) |
|
||||
| 5 | `OBJ_CODE` | Compiled code |
|
||||
| 6 | `OBJ_FRAME` | Stack frame for closures |
|
||||
| 7 | `OBJ_FORWARD` | Forwarding pointer (GC) |
|
||||
|
||||
### Flags (bits 3-7)
|
||||
|
||||
- **Bit 3 (S)** — Stone flag. If set, the object is immutable and excluded from GC.
|
||||
- **Bit 4 (P)** — Properties flag.
|
||||
- **Bit 5 (A)** — Array flag.
|
||||
- **Bit 7 (R)** — Reserved.
|
||||
|
||||
### Capacity (bits 8-63)
|
||||
|
||||
The interpretation of the 56-bit capacity field depends on the object type.
|
||||
|
||||
## Array
|
||||
|
||||
```c
|
||||
struct JSArray {
|
||||
objhdr_t header; // type=0, capacity=element slots
|
||||
word_t len; // current number of elements
|
||||
JSValue values[]; // inline flexible array
|
||||
};
|
||||
```
|
||||
|
||||
Capacity is the number of JSValue slots allocated. Length is the number currently in use. Arrays grow by reallocating with a larger capacity.
|
||||
|
||||
## Blob
|
||||
|
||||
```c
|
||||
struct JSBlob {
|
||||
objhdr_t header; // type=1, capacity=allocated bits
|
||||
word_t length; // length in bits
|
||||
uint8_t bits[]; // bit-packed data
|
||||
};
|
||||
```
|
||||
|
||||
Blobs are bit-addressable. The length field tracks the exact number of bits written. A blob starts as antestone (mutable) for writing, then becomes stone (immutable) for reading.
|
||||
|
||||
## Text
|
||||
|
||||
```c
|
||||
struct JSText {
|
||||
objhdr_t header; // type=2, capacity=character slots
|
||||
word_t length; // length in codepoints (or hash if stoned)
|
||||
word_t packed[]; // two UTF-32 chars per 64-bit word
|
||||
};
|
||||
```
|
||||
|
||||
Text is stored as UTF-32, with two 32-bit codepoints packed per 64-bit word. When a text object is stoned, the length field is repurposed to cache the hash value (computed via `fash64`), since stoned text is immutable and the hash never changes.
|
||||
|
||||
## Record
|
||||
|
||||
```c
|
||||
struct JSRecord {
|
||||
objhdr_t header; // type=3, capacity=hash table slots
|
||||
JSRecord *proto; // prototype chain pointer
|
||||
word_t len; // number of entries
|
||||
slot slots[]; // key-value pairs (hash table)
|
||||
};
|
||||
```
|
||||
|
||||
Records use a hash table with linear probing. Slot 0 is reserved for internal metadata (class ID and record ID). Empty slots use `JS_NULL` as the key; deleted slots use `JS_EXCEPTION` as a tombstone.
|
||||
|
||||
The prototype chain is a linked list of JSRecord pointers, traversed during property lookup.
|
||||
|
||||
## Function
|
||||
|
||||
```c
|
||||
struct JSFunction {
|
||||
objhdr_t header; // type=4
|
||||
JSValue name; // function name
|
||||
int16_t length; // arity (-1 for variadic)
|
||||
uint8_t kind; // C, register, or mcode
|
||||
union {
|
||||
struct { ... } cfunc; // C function pointer
|
||||
struct { ... } regvm; // register VM code
|
||||
struct { ... } mcode; // mcode IR
|
||||
} u;
|
||||
};
|
||||
```
|
||||
|
||||
The kind field selects which union variant is active. Functions can be implemented in C (native), register code (mach VM), or mcode (JSON interpreter).
|
||||
|
||||
## Frame
|
||||
|
||||
```c
|
||||
struct JSFrame {
|
||||
objhdr_t header; // type=6, capacity=slot count
|
||||
JSValue function; // owning function
|
||||
JSValue caller; // parent frame
|
||||
uint32_t return_pc; // return address
|
||||
JSValue slots[]; // [this][args][captured][locals][temps]
|
||||
};
|
||||
```
|
||||
|
||||
Frames capture the execution context for closures. The slots array contains the function's `this` binding, arguments, captured upvalues, local variables, and temporaries. Frames are linked via the caller field for upvalue resolution across closure depth.
|
||||
|
||||
## Forwarding Pointer
|
||||
|
||||
```
|
||||
[pointer: 61 bits][111]
|
||||
```
|
||||
|
||||
During garbage collection, when an object is copied to the new heap, the old header is replaced with a forwarding pointer to the new location. This is type 7 (`OBJ_FORWARD`) and stores the new address in bits 3-63. See [Garbage Collection](#gc) for details.
|
||||
|
||||
## Object Sizing
|
||||
|
||||
All objects are aligned to 8 bytes. The total size in bytes for each type:
|
||||
|
||||
| Type | Size |
|
||||
|------|------|
|
||||
| Array | `8 + 8 + capacity * 8` |
|
||||
| Blob | `8 + 8 + ceil(capacity / 8)` |
|
||||
| Text | `8 + 8 + ceil(capacity / 2) * 8` |
|
||||
| Record | `8 + 8 + 8 + (capacity + 1) * 16` |
|
||||
| Function | `sizeof(JSFunction)` (fixed) |
|
||||
| Code | `sizeof(JSFunctionBytecode)` (fixed) |
|
||||
| Frame | `8 + 8 + 8 + 4 + capacity * 8` |
|
||||
124
docs/spec/pipeline.md
Normal file
124
docs/spec/pipeline.md
Normal file
@@ -0,0 +1,124 @@
|
||||
---
|
||||
title: "Compilation Pipeline"
|
||||
description: "Overview of the compilation stages and optimizations"
|
||||
---
|
||||
|
||||
## Overview
|
||||
|
||||
The compilation pipeline transforms source code through several stages, each adding information or lowering the representation toward execution. All backends share the same path through mcode and streamline. There are three execution backends: the Mach register VM (default), the Mcode interpreter (debug), and native code via QBE (experimental).
|
||||
|
||||
```
|
||||
Source → Tokenize → Parse → Fold → Mcode → Streamline → Mach VM (default)
|
||||
→ Mcode Interpreter
|
||||
→ QBE → Native
|
||||
```
|
||||
|
||||
## Stages
|
||||
|
||||
### Tokenize (`tokenize.cm`)
|
||||
|
||||
Splits source text into tokens. Handles string interpolation by re-tokenizing template literal contents. Produces a token array with position information (line, column).
|
||||
|
||||
### Parse (`parse.cm`)
|
||||
|
||||
Converts tokens into an AST. Also performs semantic analysis:
|
||||
|
||||
- **Scope records**: For each scope (global, function), builds a record mapping variable names to their metadata: `make` (var/def/function/input), `function_nr`, `nr_uses`, `closure` flag, and `level`.
|
||||
- **Type tags**: When the right-hand side of a `def` is a syntactically obvious type, stamps `type_tag` on the scope record entry. Derivable types: `"integer"`, `"number"`, `"text"`, `"array"`, `"record"`, `"function"`, `"logical"`, `"null"`.
|
||||
- **Intrinsic resolution**: Names used but not locally bound are recorded in `ast.intrinsics`. Name nodes referencing intrinsics get `intrinsic: true`.
|
||||
- **Access kind**: Subscript (`[`) nodes get `access_kind`: `"index"` for numeric subscripts, `"field"` for string subscripts, omitted otherwise.
|
||||
- **Tail position**: Return statements where the expression is a call get `tail: true`.
|
||||
|
||||
### Fold (`fold.cm`)
|
||||
|
||||
Operates on the AST. Performs constant folding and type analysis:
|
||||
|
||||
- **Constant folding**: Evaluates arithmetic on known constants at compile time (e.g., `5 + 10` becomes `15`).
|
||||
- **Constant propagation**: Tracks `def` bindings whose values are known constants.
|
||||
- **Type propagation**: Extends `type_tag` through operations. When both operands of an arithmetic op have known types, the result type is known. Propagates type tags to reference sites.
|
||||
- **Intrinsic specialization**: When an intrinsic call's argument types are known, stamps a `hint` on the call node. For example, `length(x)` where x is a known array gets `hint: "array_length"`. Type checks like `is_array(known_array)` are folded to `true`.
|
||||
- **Purity marking**: Stamps `pure: true` on expressions with no side effects (literals, name references, arithmetic on pure operands).
|
||||
- **Dead code elimination**: Removes unreachable branches when conditions are known constants.
|
||||
|
||||
### Mcode (`mcode.cm`)
|
||||
|
||||
Lowers the AST to a JSON-based intermediate representation with explicit operations. Key design principle: **every type check is an explicit instruction** so downstream optimizers can see and eliminate them.
|
||||
|
||||
- **Typed load/store**: Emits `load_index` (array by integer), `load_field` (record by string), or `load_dynamic` (unknown) based on type information from fold.
|
||||
- **Decomposed calls**: Function calls are split into `frame` (create call frame) + `setarg` (set arguments) + `invoke` (execute call).
|
||||
- **Intrinsic access**: Intrinsic functions are loaded via `access` with an intrinsic marker rather than global lookup.
|
||||
|
||||
See [Mcode IR](mcode.md) for instruction format details.
|
||||
|
||||
### Streamline (`streamline.cm`)
|
||||
|
||||
Optimizes the Mcode IR. Operates per-function:
|
||||
|
||||
- **Redundant instruction elimination**: Removes no-op patterns and redundant moves.
|
||||
- **Dead code removal**: Eliminates instructions whose results are never used.
|
||||
- **Type-based narrowing**: When type information is available, narrows `load_dynamic`/`store_dynamic` to typed variants.
|
||||
|
||||
### QBE Emit (`qbe_emit.cm`)
|
||||
|
||||
Lowers optimized Mcode IR to QBE intermediate language for native code compilation. Each Mcode function becomes a QBE function that calls into the cell runtime (`cell_rt_*` functions) for operations that require the runtime (allocation, intrinsic dispatch, etc.).
|
||||
|
||||
String constants are interned in a data section. Integer constants are NaN-boxed inline.
|
||||
|
||||
### QBE Macros (`qbe.cm`)
|
||||
|
||||
Provides operation implementations as QBE IL templates. Each arithmetic, comparison, and type operation is defined as a function that emits the corresponding QBE instructions, handling type dispatch (integer, float, text paths) with proper guard checks.
|
||||
|
||||
## Execution Backends
|
||||
|
||||
### Mach VM (default)
|
||||
|
||||
Binary 32-bit register VM. The Mach serializer (`mach.c`) converts streamlined mcode JSON into compact 32-bit bytecode with a constant pool. Used for production execution and bootstrapping.
|
||||
|
||||
```
|
||||
./cell script.ce
|
||||
```
|
||||
|
||||
Debug the mach bytecode output:
|
||||
|
||||
```
|
||||
./cell --core . --dump-mach script.ce
|
||||
```
|
||||
|
||||
### Mcode Interpreter
|
||||
|
||||
JSON-based interpreter. Used for debugging the compilation pipeline.
|
||||
|
||||
```
|
||||
./cell --mcode script.ce
|
||||
```
|
||||
|
||||
### QBE Native (experimental)
|
||||
|
||||
Generates QBE IL that can be compiled to native code.
|
||||
|
||||
```
|
||||
./cell --emit-qbe script.ce > output.ssa
|
||||
```
|
||||
|
||||
## Files
|
||||
|
||||
| File | Role |
|
||||
|------|------|
|
||||
| `tokenize.cm` | Lexer |
|
||||
| `parse.cm` | Parser + semantic analysis |
|
||||
| `fold.cm` | Constant folding + type analysis |
|
||||
| `mcode.cm` | AST → Mcode IR lowering |
|
||||
| `streamline.cm` | Mcode IR optimizer |
|
||||
| `qbe_emit.cm` | Mcode IR → QBE IL emitter |
|
||||
| `qbe.cm` | QBE IL operation templates |
|
||||
| `internal/bootstrap.cm` | Pipeline orchestrator |
|
||||
|
||||
## Test Files
|
||||
|
||||
| File | Tests |
|
||||
|------|-------|
|
||||
| `parse_test.ce` | Type tags, access_kind, intrinsic resolution |
|
||||
| `fold_test.ce` | Type propagation, purity, intrinsic hints |
|
||||
| `mcode_test.ce` | Typed load/store, decomposed calls |
|
||||
| `streamline_test.ce` | Optimization counts, IR before/after |
|
||||
| `qbe_test.ce` | End-to-end QBE IL generation |
|
||||
82
docs/spec/stone.md
Normal file
82
docs/spec/stone.md
Normal file
@@ -0,0 +1,82 @@
|
||||
---
|
||||
title: "Stone Memory"
|
||||
description: "Immutable arena allocation"
|
||||
---
|
||||
|
||||
## Overview
|
||||
|
||||
Stone memory is a separate allocation arena for immutable values. Objects in stone memory are permanent — they are never moved, never freed, and never touched by the garbage collector.
|
||||
|
||||
The `stone()` function in ƿit petrifies a value, deeply freezing it and all its descendants. Stoned objects have the S bit set in their object header.
|
||||
|
||||
## The Stone Arena
|
||||
|
||||
Stone memory uses bump allocation from a contiguous arena:
|
||||
|
||||
```
|
||||
stone_base ──────── stone_free ──────── stone_end
|
||||
[allocated objects] [free space ]
|
||||
```
|
||||
|
||||
Allocation advances `stone_free` forward. When the arena is exhausted, overflow pages are allocated via the system allocator and linked together:
|
||||
|
||||
```c
|
||||
struct StonePage {
|
||||
struct StonePage *next;
|
||||
size_t size;
|
||||
uint8_t data[];
|
||||
};
|
||||
```
|
||||
|
||||
## The S Bit
|
||||
|
||||
Bit 3 of the object header is the stone flag. When set:
|
||||
|
||||
- The object is **immutable** — writes disrupt
|
||||
- The object is **excluded from GC** — the collector skips it entirely
|
||||
- For text objects, the length field caches the **hash** instead of the character count (since the text cannot change, the hash is computed once and reused)
|
||||
|
||||
## What Gets Stoned
|
||||
|
||||
When `stone(value)` is called:
|
||||
|
||||
1. If the value is already stone, return immediately
|
||||
2. Recursively walk all nested values (array elements, record fields, etc.)
|
||||
3. Copy each mutable object into the stone arena
|
||||
4. Set the S bit on each copied object
|
||||
5. Return the stoned value
|
||||
|
||||
The operation is deep — an entire object graph becomes permanently immutable.
|
||||
|
||||
## Text Interning
|
||||
|
||||
The stone arena maintains a hash table for text interning. When a text value is stoned, it is looked up in the intern table. If an identical string already exists in stone memory, the existing one is reused. This deduplicates strings and makes equality comparison O(1) for stoned text.
|
||||
|
||||
The hash is computed with `fash64` over the packed UTF-32 words.
|
||||
|
||||
## Usage Patterns
|
||||
|
||||
### Module Return Values
|
||||
|
||||
Every module's return value is automatically stoned:
|
||||
|
||||
```javascript
|
||||
// config.cm
|
||||
return {
|
||||
debug: true,
|
||||
timeout: 30
|
||||
}
|
||||
// The returned object is stone — shared safely between actors
|
||||
```
|
||||
|
||||
### Message Passing
|
||||
|
||||
Messages between actors are stoned before delivery, ensuring actors never share mutable state.
|
||||
|
||||
### Constants
|
||||
|
||||
Literal objects and arrays that can be determined at compile time may be allocated directly in stone memory.
|
||||
|
||||
## Relationship to GC
|
||||
|
||||
The Cheney copying collector only operates on the mutable heap. During collection, when the collector encounters a pointer to stone memory (S bit set), it skips it — stone objects are roots that never move. This means stone memory acts as a permanent root set with zero GC overhead.
|
||||
96
docs/spec/values.md
Normal file
96
docs/spec/values.md
Normal file
@@ -0,0 +1,96 @@
|
||||
---
|
||||
title: "Value Representation"
|
||||
description: "JSValue tagging and encoding"
|
||||
---
|
||||
|
||||
## Overview
|
||||
|
||||
Every value in ƿit is a 64-bit word called a JSValue. The runtime uses LSB (least significant bit) tagging to pack type information directly into the value, avoiding heap allocation for common types.
|
||||
|
||||
## Tag Encoding
|
||||
|
||||
The lowest bits of a JSValue determine its type:
|
||||
|
||||
| LSB Pattern | Type | Payload |
|
||||
|-------------|------|---------|
|
||||
| `xxxxxxx0` | Integer | 31-bit signed integer in upper bits |
|
||||
| `xxxxx001` | Pointer | 61-bit aligned heap pointer |
|
||||
| `xxxxx101` | Short float | 8-bit exponent + 52-bit mantissa |
|
||||
| `xxxxx011` | Special | 5-bit tag selects subtype |
|
||||
|
||||
### Integers
|
||||
|
||||
If the least significant bit is 0, the value is an immediate 31-bit signed integer. The integer is stored in the upper bits, extracted via `v >> 1`.
|
||||
|
||||
```
|
||||
[integer: 31 bits][0]
|
||||
```
|
||||
|
||||
Range: -1073741824 to 1073741823. Numbers outside this range are stored as short floats or heap-allocated.
|
||||
|
||||
### Pointers
|
||||
|
||||
If the lowest 3 bits are `001`, the value is a pointer to a heap object. The pointer is 8-byte aligned, so the low 3 bits are available for the tag. The actual address is extracted by clearing the low 3 bits.
|
||||
|
||||
```
|
||||
[pointer: 61 bits][001]
|
||||
```
|
||||
|
||||
All heap objects (arrays, records, blobs, text, functions, etc.) are referenced through pointer-tagged JSValues.
|
||||
|
||||
### Short Floats
|
||||
|
||||
If the lowest 3 bits are `101`, the value encodes a floating-point number directly. The format uses an 8-bit exponent (bias 127) and 52-bit mantissa, similar to IEEE 754 but with reduced range.
|
||||
|
||||
```
|
||||
[sign: 1][exponent: 8][mantissa: 52][101]
|
||||
```
|
||||
|
||||
Range: approximately ±3.4 * 10^38. Numbers outside this range fall back to null. Zero is always positive zero.
|
||||
|
||||
### Specials
|
||||
|
||||
If the lowest 2 bits are `11`, the next 3 bits select a special type:
|
||||
|
||||
| 5-bit Tag | Value |
|
||||
|-----------|-------|
|
||||
| `00011` | Boolean (true/false in upper bits) |
|
||||
| `00111` | Null |
|
||||
| `01111` | Exception marker |
|
||||
| `10111` | Uninitialized |
|
||||
| `11011` | Immediate string |
|
||||
| `11111` | Catch offset |
|
||||
|
||||
## Immediate Strings
|
||||
|
||||
Short ASCII strings (up to 7 characters) are packed directly into the JSValue without heap allocation:
|
||||
|
||||
```
|
||||
[char6][char5][char4][char3][char2][char1][char0][length: 3][11011]
|
||||
```
|
||||
|
||||
Each character occupies 8 bits. The length (0-7) is stored in bits 5-7. Only ASCII characters (0-127) qualify — any non-ASCII character forces heap allocation.
|
||||
|
||||
```javascript
|
||||
var s = "hello" // 5 chars, fits in immediate string
|
||||
var t = "" // immediate (length 0)
|
||||
var u = "longtext" // 8 chars, heap-allocated
|
||||
```
|
||||
|
||||
## Null
|
||||
|
||||
Null is encoded as a special-tagged value with tag `00111`. There is no `undefined` in ƿit — only null.
|
||||
|
||||
```javascript
|
||||
var x = null // special tag null
|
||||
var y = 1 / 0 // also null (division by zero)
|
||||
var z = {}.missing // null (missing field)
|
||||
```
|
||||
|
||||
## Boolean
|
||||
|
||||
True and false are encoded as specials with tag `00011`, distinguished by a bit in the upper payload.
|
||||
|
||||
## Summary
|
||||
|
||||
The tagging scheme ensures that the most common values — small integers, booleans, null, and short strings — require zero heap allocation. This significantly reduces GC pressure and improves cache locality.
|
||||
119
docs/wota.md
Normal file
119
docs/wota.md
Normal file
@@ -0,0 +1,119 @@
|
||||
---
|
||||
title: "Wota Format"
|
||||
description: "Word Object Transfer Arrangement"
|
||||
weight: 86
|
||||
type: "docs"
|
||||
---
|
||||
|
||||
Wota is a binary message format for local inter-process communication. It is similar to Nota but works at word granularity (64-bit words) rather than byte granularity. Wota arrangements are less compact than Nota but faster to arrange and consume.
|
||||
|
||||
Wota stands for Word Object Transfer Arrangement.
|
||||
|
||||
## Type Summary
|
||||
|
||||
| Byte | Type |
|
||||
|------|------|
|
||||
| `00` | Integer |
|
||||
| `01` | Floating Point |
|
||||
| `02` | Array |
|
||||
| `03` | Record |
|
||||
| `04` | Blob |
|
||||
| `05` | Text |
|
||||
| `07` | Symbol |
|
||||
|
||||
## Preambles
|
||||
|
||||
Every Wota value starts with a preamble word. The least significant byte contains the type. The remaining 56 bits contain type-specific data.
|
||||
|
||||
## Blob
|
||||
|
||||
A blob is a string of bits. The remaining field contains the number of bits. The number of words that follow: `floor((number_of_bits + 63) / 64)`. The first bit of the blob goes into the most significant bit of the first word. The final word is padded with 0.
|
||||
|
||||
Example: A blob containing 25 bits `111100001110001100100001`:
|
||||
|
||||
```
|
||||
0000000000001904 # preamble: 25 bits, type blob
|
||||
F0E3208000000000 # data (padded to 64 bits)
|
||||
```
|
||||
|
||||
## Text
|
||||
|
||||
The text is a string of UTF-32 characters packed 2 per word. The remaining field contains the number of characters. The number of words that follow: `floor((number_of_characters + 1) / 2)`. The final word is padded with 0.
|
||||
|
||||
Example: `"cat"`:
|
||||
|
||||
```
|
||||
0000000000000305 # preamble: 3 characters, type text
|
||||
0000006300000061 # 'c' and 'a'
|
||||
0000007400000000 # 't' and padding
|
||||
```
|
||||
|
||||
## Array
|
||||
|
||||
An array is an ordered sequence of values. The remaining field contains the number of elements. Following the preamble are the elements, each beginning with its own preamble. Nesting is encouraged. Cyclic structures are not allowed.
|
||||
|
||||
Example: `["duck", "dragon"]`:
|
||||
|
||||
```
|
||||
0000000000000202 # preamble: 2 elements, type array
|
||||
0000000000000405 # text "duck": 4 chars
|
||||
0000006400000074 # 'd' 't' (reversed pair order)
|
||||
000000630000006B # 'c' 'k'
|
||||
0000000000000605 # text "dragon": 6 chars
|
||||
0000006400000072 # 'd' 'r'
|
||||
0000006100000067 # 'a' 'g'
|
||||
0000006F0000006E # 'o' 'n'
|
||||
```
|
||||
|
||||
## Record
|
||||
|
||||
A record is a set of key/value pairs. Keys must be text. The remaining field contains the number of pairs.
|
||||
|
||||
Example: `{"ox": ["O", "X"]}`:
|
||||
|
||||
```
|
||||
0000000000000103 # preamble: 1 pair, type record
|
||||
0000000000000205 # key "ox": 2 chars
|
||||
0000006F00000078 # 'o' 'x'
|
||||
0000000000000202 # value: array of 2
|
||||
0000000000000105 # "O": 1 char
|
||||
0000004F00000000 # 'O'
|
||||
0000000000000105 # "X": 1 char
|
||||
0000005800000000 # 'X'
|
||||
```
|
||||
|
||||
## Number
|
||||
|
||||
Numbers are represented as DEC64. To arrange an integer, shift the integer up 8 bits. The number is incorporated directly into the preamble.
|
||||
|
||||
Example: `7`:
|
||||
|
||||
```
|
||||
0000000000000700 # integer 7 as DEC64
|
||||
```
|
||||
|
||||
To arrange a floating point number, place the number in the word following the floating point preamble.
|
||||
|
||||
Example: `4.25`:
|
||||
|
||||
```
|
||||
0000000000000001 # preamble: type floating point
|
||||
000000000001A9FE # DEC64 encoding of 4.25
|
||||
```
|
||||
|
||||
Care must be taken when decoding that the least significant byte of the number is not `80` (the null exponent).
|
||||
|
||||
## Symbol
|
||||
|
||||
The remaining field contains the symbol.
|
||||
|
||||
Example: `[null, false, true, private, system]`:
|
||||
|
||||
```
|
||||
0000000000000502 # array of 5
|
||||
0000000000000007 # null
|
||||
0000000000000207 # false
|
||||
0000000000000307 # true
|
||||
0000000000000807 # private
|
||||
0000000000000907 # system
|
||||
```
|
||||
20
dump_mcode.cm
Normal file
20
dump_mcode.cm
Normal file
@@ -0,0 +1,20 @@
|
||||
var fd = use("fd")
|
||||
var json = use("json")
|
||||
var tokenize = use("tokenize")
|
||||
var parse = use("parse")
|
||||
var fold = use("fold")
|
||||
var mcode = use("mcode")
|
||||
var streamline = use("streamline")
|
||||
|
||||
var name = args[0]
|
||||
var src = text(fd.slurp(name))
|
||||
var tok = tokenize(src, name)
|
||||
var ast = parse(tok.tokens, src, name, tokenize)
|
||||
var folded = fold(ast)
|
||||
var compiled = mcode(folded)
|
||||
var optimized = streamline(compiled)
|
||||
var out = json.encode(optimized)
|
||||
var f = fd.open("/tmp/mcode_dump.json", "w")
|
||||
fd.write(f, out)
|
||||
fd.close(f)
|
||||
print("wrote /tmp/mcode_dump.json")
|
||||
251
editors/ai/pit-context.md
Normal file
251
editors/ai/pit-context.md
Normal file
@@ -0,0 +1,251 @@
|
||||
# ƿit Language — AI Context
|
||||
|
||||
ƿit (pronounced "pit") is a safe, actor-based programming language. Its syntax resembles JavaScript but with significant differences. Scripts use `.ce` (actors) and `.cm` (modules) file extensions.
|
||||
|
||||
## Key Differences from JavaScript
|
||||
|
||||
- **`var` / `def`** — `var` is mutable, `def` is constant. No `let` or `const`.
|
||||
- **`==` is strict** — No `===` or `!==`. `==` and `!=` are always strict comparison.
|
||||
- **No `undefined`** — Only `null`. Division by zero produces `null`, not `Infinity`.
|
||||
- **No classes** — Use `meme()`, `proto()`, `isa()` for prototype chains.
|
||||
- **No `for...in`, `for...of`, spread, rest, or default params.**
|
||||
- **Variables declared at function body level only** — Not inside `if`/`while`/`for` blocks.
|
||||
- **All variables must be initialized** — `var x` alone is an error; use `var x = null`.
|
||||
- **`disrupt` / `disruption`** — No `try`/`catch`/`throw`. Error handling uses:
|
||||
```javascript
|
||||
var fn = function() {
|
||||
disrupt // raise an error (bare keyword, no value)
|
||||
} disruption {
|
||||
// handle the error
|
||||
}
|
||||
```
|
||||
- **No arraybuffers** — Use `blob` (works with bits; `stone(blob)` before reading).
|
||||
- **Identifiers can contain `?` and `!`** — e.g., `nil?`, `set!`, `is?valid`.
|
||||
- **4-parameter limit** — Functions take at most 4 named parameters.
|
||||
- **Everything lowercase** — Convention is all-lowercase identifiers with underscores.
|
||||
|
||||
## Variable Declaration
|
||||
|
||||
```javascript
|
||||
var count = 0 // mutable
|
||||
def MAX = 100 // constant (cannot be reassigned)
|
||||
var x = null // must initialize (var x alone is an error)
|
||||
```
|
||||
|
||||
## Functions
|
||||
|
||||
```javascript
|
||||
var greet = function(name) {
|
||||
print(`hello ${name}`)
|
||||
}
|
||||
|
||||
// Arrow functions
|
||||
var double = x => x * 2
|
||||
var add = (a, b) => a + b
|
||||
```
|
||||
|
||||
## Push / Pop Syntax
|
||||
|
||||
```javascript
|
||||
var a = [1, 2]
|
||||
a[] = 3 // push: a is now [1, 2, 3]
|
||||
var v = a[] // pop: v is 3, a is [1, 2]
|
||||
```
|
||||
|
||||
## Control Flow
|
||||
|
||||
```javascript
|
||||
if (x > 0) {
|
||||
print("positive")
|
||||
} else {
|
||||
print("non-positive")
|
||||
}
|
||||
|
||||
while (i < 10) {
|
||||
i = i + 1
|
||||
}
|
||||
|
||||
for (var i = 0; i < 10; i = i + 1) {
|
||||
print(i)
|
||||
}
|
||||
|
||||
// do-while
|
||||
do {
|
||||
i = i + 1
|
||||
} while (i < 10)
|
||||
```
|
||||
|
||||
## Error Handling
|
||||
|
||||
```javascript
|
||||
var safe_divide = function(a, b) {
|
||||
if (b == 0) {
|
||||
disrupt
|
||||
}
|
||||
return a / b
|
||||
} disruption {
|
||||
return null
|
||||
}
|
||||
```
|
||||
|
||||
## Creator Functions (Polymorphic)
|
||||
|
||||
These examine argument types to decide behavior:
|
||||
|
||||
### array()
|
||||
- `array(5)` — `[null, null, null, null, null]`
|
||||
- `array(3, 0)` — `[0, 0, 0]`
|
||||
- `array(5, i => i * 2)` — `[0, 2, 4, 6, 8]`
|
||||
- `array([1,2])` — copy
|
||||
- `array([1,2,3], x => x * 10)` — map: `[10, 20, 30]`
|
||||
- `array([1,2], [3,4])` — concat: `[1, 2, 3, 4]`
|
||||
- `array([1,2,3,4,5], 1, 4)` — slice: `[2, 3, 4]`
|
||||
- `array({a: 1, b: 2})` — keys: `["a", "b"]`
|
||||
- `array("hello")` — characters: `["h", "e", "l", "l", "o"]`
|
||||
- `array("a,b,c", ",")` — split: `["a", "b", "c"]`
|
||||
|
||||
### text()
|
||||
- `text([1, 2, 3], ", ")` — join: `"1, 2, 3"`
|
||||
- `text(255, 16)` — radix: `"ff"`
|
||||
- `text("hello", 0, 3)` — substring: `"hel"`
|
||||
|
||||
### number()
|
||||
- `number("42")` — parse: `42`
|
||||
- `number("ff", 16)` — radix: `255`
|
||||
- `number(true)` — `1`
|
||||
|
||||
### record()
|
||||
- `record({a: 1})` — copy
|
||||
- `record({a: 1}, {b: 2})` — merge: `{a: 1, b: 2}`
|
||||
- `record(["x", "y"])` — from keys: `{x: true, y: true}`
|
||||
|
||||
## All Intrinsic Functions
|
||||
|
||||
**Constants:** `false`, `true`, `null`, `pi`
|
||||
|
||||
**Type checks:** `is_array`, `is_blob`, `is_character`, `is_data`, `is_digit`, `is_false`, `is_fit`, `is_function`, `is_integer`, `is_letter`, `is_logical`, `is_lower`, `is_null`, `is_number`, `is_object`, `is_pattern`, `is_stone`, `is_text`, `is_true`, `is_upper`, `is_whitespace`
|
||||
|
||||
**Creators:** `array`, `logical`, `number`, `record`, `text`
|
||||
|
||||
**Math:** `abs`, `ceiling`, `floor`, `fraction`, `max`, `min`, `modulo`, `neg`, `remainder`, `round`, `sign`, `trunc`, `whole`
|
||||
|
||||
**Text:** `character`, `codepoint`, `ends_with`, `extract`, `format`, `lower`, `normalize`, `replace`, `search`, `starts_with`, `trim`, `upper`
|
||||
|
||||
**Array:** `every`, `filter`, `find`, `for`, `length`, `reduce`, `reverse`, `some`, `sort`
|
||||
|
||||
**Objects:** `meme`, `proto`, `isa`, `stone`
|
||||
|
||||
**Functions:** `apply`, `splat`
|
||||
|
||||
**I/O:** `print`
|
||||
|
||||
**Async:** `fallback`, `parallel`, `race`, `sequence`
|
||||
|
||||
**Misc:** `logical`, `not`, `use`
|
||||
|
||||
## Variable Scoping
|
||||
|
||||
Variables are scoped to the function body in which they are declared. There is no block scoping. All declarations must be at the top level of a function body (not nested inside `if`/`while`/`for`).
|
||||
|
||||
```javascript
|
||||
var outer = function() {
|
||||
var x = 10
|
||||
var inner = function() {
|
||||
// x is visible here via closure
|
||||
print(x)
|
||||
}
|
||||
inner()
|
||||
}
|
||||
```
|
||||
|
||||
## Modules (.cm files)
|
||||
|
||||
Modules return a value (typically a record of exports). They are loaded with `use()`, cached, and frozen.
|
||||
|
||||
```javascript
|
||||
// math_utils.cm
|
||||
var square = x => x * x
|
||||
var cube = x => x * x * x
|
||||
return {square: square, cube: cube}
|
||||
|
||||
// main.ce
|
||||
var utils = use('math_utils')
|
||||
print(utils.square(5)) // 25
|
||||
```
|
||||
|
||||
## Standard Library (loaded with use())
|
||||
|
||||
- `blob` — binary data (works with bits, not bytes)
|
||||
- `time` — time constants and conversions
|
||||
- `math` — trig, logarithms, roots (sub-modules: `math/radians`, `math/turns`)
|
||||
- `json` — JSON encoding/decoding (`json.encode`, `json.decode`)
|
||||
- `random` — random number generation
|
||||
- `fd` — file descriptor operations (`fd.read`, `fd.write`, `fd.slurp`, `fd.stat`)
|
||||
|
||||
## Actor Model (.ce files)
|
||||
|
||||
Actors are independent execution units that never share memory. They communicate via message passing.
|
||||
|
||||
```javascript
|
||||
// greeter.ce
|
||||
$receiver(function(msg) {
|
||||
$send(msg.from, {greeting: `hello ${msg.name}`})
|
||||
})
|
||||
```
|
||||
|
||||
### Actor Intrinsics ($ prefix)
|
||||
|
||||
- `$me` — this actor's address
|
||||
- `$send(address, message)` — send a message
|
||||
- `$start(script, env)` — start a new actor
|
||||
- `$stop()` — stop this actor
|
||||
- `$delay(ms)` — delay processing
|
||||
- `$receiver(fn)` — set message handler
|
||||
- `$clock(interval, message)` — periodic self-message
|
||||
- `$portal(name)` — create named portal
|
||||
- `$contact(name)` — connect to portal
|
||||
- `$couple(address)` — lifecycle coupling
|
||||
- `$unneeded(fn)` — cleanup callback
|
||||
- `$connection(address)` — establish connection
|
||||
- `$time_limit(ms)` — execution time limit
|
||||
|
||||
## Common Patterns
|
||||
|
||||
### Iteration
|
||||
```javascript
|
||||
// Preferred: use for() intrinsic
|
||||
for([1, 2, 3], function(item, index) {
|
||||
print(`${text(index)}: ${text(item)}`)
|
||||
})
|
||||
|
||||
// C-style for loop
|
||||
for (var i = 0; i < length(items); i = i + 1) {
|
||||
print(items[i])
|
||||
}
|
||||
```
|
||||
|
||||
### String Building
|
||||
```javascript
|
||||
// Use backtick interpolation
|
||||
var msg = `hello ${name}, you are ${text(age)} years old`
|
||||
|
||||
// Join array
|
||||
var csv = text(values, ",")
|
||||
```
|
||||
|
||||
### Record Manipulation
|
||||
```javascript
|
||||
var obj = {name: "alice", age: 30}
|
||||
var keys = array(obj) // ["name", "age"]
|
||||
var copy = record(obj) // mutable copy
|
||||
var merged = record(obj, {role: "admin"})
|
||||
```
|
||||
|
||||
### Error-Safe Operations
|
||||
```javascript
|
||||
var safe_parse = function(input) {
|
||||
return number(input)
|
||||
} disruption {
|
||||
return null
|
||||
}
|
||||
```
|
||||
30
editors/vscode/language-configuration.json
Normal file
30
editors/vscode/language-configuration.json
Normal file
@@ -0,0 +1,30 @@
|
||||
{
|
||||
"comments": {
|
||||
"lineComment": "//",
|
||||
"blockComment": ["/*", "*/"]
|
||||
},
|
||||
"brackets": [
|
||||
["{", "}"],
|
||||
["[", "]"],
|
||||
["(", ")"]
|
||||
],
|
||||
"autoClosingPairs": [
|
||||
{ "open": "{", "close": "}" },
|
||||
{ "open": "[", "close": "]" },
|
||||
{ "open": "(", "close": ")" },
|
||||
{ "open": "\"", "close": "\"", "notIn": ["string"] },
|
||||
{ "open": "`", "close": "`", "notIn": ["string"] }
|
||||
],
|
||||
"surroundingPairs": [
|
||||
["{", "}"],
|
||||
["[", "]"],
|
||||
["(", ")"],
|
||||
["\"", "\""],
|
||||
["`", "`"]
|
||||
],
|
||||
"indentationRules": {
|
||||
"increaseIndentPattern": "^.*\\{[^}\"'`]*$",
|
||||
"decreaseIndentPattern": "^\\s*\\}"
|
||||
},
|
||||
"wordPattern": "[a-zA-Z_$][a-zA-Z0-9_$?!]*"
|
||||
}
|
||||
113
editors/vscode/lsp/analysis.cm
Normal file
113
editors/vscode/lsp/analysis.cm
Normal file
@@ -0,0 +1,113 @@
|
||||
// Document analysis module.
|
||||
// Call make(tokenize_mod, parse_mod) to get an analysis object.
|
||||
|
||||
var json = use('json')
|
||||
|
||||
// Create an analysis module bound to the tokenize and parse functions.
|
||||
var make = function(tokenize_mod, parse_mod) {
|
||||
|
||||
// Tokenize and parse a document, storing the results.
|
||||
var update = function(docs, uri, params) {
|
||||
var src = params.src
|
||||
var version = params.version
|
||||
var tok_result = null
|
||||
var ast = null
|
||||
var errors = []
|
||||
var doc = null
|
||||
|
||||
var do_tokenize = function() {
|
||||
tok_result = tokenize_mod(src, uri)
|
||||
} disruption {
|
||||
errors = [{message: "Tokenize failed", line: 1, column: 1}]
|
||||
}
|
||||
var do_parse = function() {
|
||||
ast = parse_mod(tok_result.tokens, src, uri, tokenize_mod)
|
||||
} disruption {
|
||||
// parse_mod may set errors on ast even on partial failure
|
||||
}
|
||||
|
||||
do_tokenize()
|
||||
|
||||
if (tok_result != null) {
|
||||
do_parse()
|
||||
|
||||
if (ast != null && ast.errors != null) {
|
||||
errors = ast.errors
|
||||
}
|
||||
}
|
||||
|
||||
doc = {
|
||||
uri: uri,
|
||||
text: src,
|
||||
version: version,
|
||||
tokens: (tok_result != null) ? tok_result.tokens : [],
|
||||
ast: ast,
|
||||
errors: errors
|
||||
}
|
||||
docs[uri] = doc
|
||||
return doc
|
||||
}
|
||||
|
||||
// Remove a document from the store.
|
||||
var remove = function(docs, uri) {
|
||||
delete docs[uri]
|
||||
}
|
||||
|
||||
// Convert parse errors to LSP diagnostics.
|
||||
var diagnostics = function(doc) {
|
||||
var result = []
|
||||
var _i = 0
|
||||
var e = null
|
||||
var line = null
|
||||
var col = null
|
||||
while (_i < length(doc.errors)) {
|
||||
e = doc.errors[_i]
|
||||
line = (e.line != null) ? e.line - 1 : 0
|
||||
col = (e.column != null) ? e.column - 1 : 0
|
||||
result[] = {
|
||||
range: {
|
||||
start: {line: line, character: col},
|
||||
end: {line: line, character: col + 1}
|
||||
},
|
||||
severity: 1,
|
||||
source: "pit",
|
||||
message: e.message
|
||||
}
|
||||
_i = _i + 1
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// Find the token at a given line/column (0-based).
|
||||
var token_at = function(doc, line, col) {
|
||||
var tokens = doc.tokens
|
||||
var _i = 0
|
||||
var tok = null
|
||||
while (_i < length(tokens)) {
|
||||
tok = tokens[_i]
|
||||
if (tok.from_row == line && tok.from_column <= col && tok.to_column >= col) {
|
||||
return tok
|
||||
}
|
||||
if (tok.from_row < line && tok.to_row > line) {
|
||||
return tok
|
||||
}
|
||||
if (tok.from_row < line && tok.to_row == line && tok.to_column >= col) {
|
||||
return tok
|
||||
}
|
||||
if (tok.from_row == line && tok.to_row > line && tok.from_column <= col) {
|
||||
return tok
|
||||
}
|
||||
_i = _i + 1
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
return {
|
||||
update: update,
|
||||
remove: remove,
|
||||
diagnostics: diagnostics,
|
||||
token_at: token_at
|
||||
}
|
||||
}
|
||||
|
||||
return make
|
||||
133
editors/vscode/lsp/completions.cm
Normal file
133
editors/vscode/lsp/completions.cm
Normal file
@@ -0,0 +1,133 @@
|
||||
// Completion provider for the ƿit LSP.
|
||||
|
||||
// CompletionItemKind constants (LSP spec)
|
||||
def KIND_FUNCTION = 3
|
||||
def KIND_VARIABLE = 6
|
||||
def KIND_KEYWORD = 14
|
||||
def KIND_CONSTANT = 21
|
||||
|
||||
// All intrinsic function names
|
||||
def intrinsic_functions = [
|
||||
"abs", "apply", "array", "ceiling", "character", "codepoint",
|
||||
"ends_with", "every", "extract", "fallback", "filter", "find",
|
||||
"floor", "format", "fraction",
|
||||
"is_array", "is_blob", "is_character", "is_data", "is_digit",
|
||||
"is_false", "is_fit", "is_function", "is_integer", "is_letter",
|
||||
"is_logical", "is_lower", "is_null", "is_number", "is_object",
|
||||
"is_pattern", "is_stone", "is_text", "is_true", "is_upper",
|
||||
"is_whitespace",
|
||||
"length", "logical", "lower", "max", "min", "modulo",
|
||||
"neg", "normalize", "not", "number",
|
||||
"parallel", "print", "race", "record", "reduce", "remainder",
|
||||
"replace", "reverse", "round",
|
||||
"search", "sequence", "sign", "some", "sort", "starts_with",
|
||||
"stone", "text", "trim", "trunc", "upper", "whole",
|
||||
"meme", "proto", "isa", "splat", "use"
|
||||
]
|
||||
|
||||
// Keywords that can be completed
|
||||
def keywords = [
|
||||
"var", "def", "if", "else", "for", "while", "do",
|
||||
"function", "return", "go", "break", "continue",
|
||||
"disrupt", "disruption", "delete", "in", "this",
|
||||
"null", "true", "false"
|
||||
]
|
||||
|
||||
// Actor intrinsics (only in .ce files)
|
||||
def actor_intrinsics = [
|
||||
"$me", "$send", "$start", "$stop", "$delay",
|
||||
"$receiver", "$clock", "$portal", "$contact",
|
||||
"$couple", "$unneeded", "$connection", "$time_limit"
|
||||
]
|
||||
|
||||
// Walk AST scopes to find variables visible at a position.
|
||||
var collect_scope_vars = function(doc, line, col) {
|
||||
var vars = []
|
||||
var ast = doc.ast
|
||||
var _i = 0
|
||||
var _j = 0
|
||||
var scope = null
|
||||
var v = null
|
||||
|
||||
if (ast == null || ast.scopes == null) {
|
||||
return vars
|
||||
}
|
||||
|
||||
// Collect variables from all scopes (simplified: return all declared vars)
|
||||
while (_i < length(ast.scopes)) {
|
||||
scope = ast.scopes[_i]
|
||||
if (scope.vars != null) {
|
||||
_j = 0
|
||||
while (_j < length(scope.vars)) {
|
||||
v = scope.vars[_j]
|
||||
if (v.name != null) {
|
||||
vars[] = {
|
||||
label: v.name,
|
||||
kind: (v.is_const == true) ? KIND_CONSTANT : KIND_VARIABLE,
|
||||
detail: (v.is_const == true) ? "def" : "var"
|
||||
}
|
||||
}
|
||||
_j = _j + 1
|
||||
}
|
||||
}
|
||||
_i = _i + 1
|
||||
}
|
||||
|
||||
return vars
|
||||
}
|
||||
|
||||
// Provide completions for a document at a position.
|
||||
var complete = function(doc, line, col) {
|
||||
var items = []
|
||||
var _i = 0
|
||||
var is_actor = ends_with(doc.uri, ".ce")
|
||||
|
||||
// Intrinsic functions
|
||||
_i = 0
|
||||
while (_i < length(intrinsic_functions)) {
|
||||
items[] = {
|
||||
label: intrinsic_functions[_i],
|
||||
kind: KIND_FUNCTION,
|
||||
detail: "intrinsic"
|
||||
}
|
||||
_i = _i + 1
|
||||
}
|
||||
|
||||
// Keywords
|
||||
_i = 0
|
||||
while (_i < length(keywords)) {
|
||||
items[] = {
|
||||
label: keywords[_i],
|
||||
kind: KIND_KEYWORD,
|
||||
detail: "keyword"
|
||||
}
|
||||
_i = _i + 1
|
||||
}
|
||||
|
||||
// Actor intrinsics (only for .ce files)
|
||||
if (is_actor) {
|
||||
_i = 0
|
||||
while (_i < length(actor_intrinsics)) {
|
||||
items[] = {
|
||||
label: actor_intrinsics[_i],
|
||||
kind: KIND_FUNCTION,
|
||||
detail: "actor intrinsic"
|
||||
}
|
||||
_i = _i + 1
|
||||
}
|
||||
}
|
||||
|
||||
// Variables from scope analysis
|
||||
var scope_vars = collect_scope_vars(doc, line, col)
|
||||
_i = 0
|
||||
while (_i < length(scope_vars)) {
|
||||
items[] = scope_vars[_i]
|
||||
_i = _i + 1
|
||||
}
|
||||
|
||||
return items
|
||||
}
|
||||
|
||||
return {
|
||||
complete: complete
|
||||
}
|
||||
461
editors/vscode/lsp/hover.cm
Normal file
461
editors/vscode/lsp/hover.cm
Normal file
@@ -0,0 +1,461 @@
|
||||
// Hover provider for the ƿit LSP.
|
||||
// Shows documentation for intrinsic functions and variable info.
|
||||
|
||||
// Intrinsic function documentation database.
|
||||
// Each entry: {signature, description}
|
||||
def intrinsic_docs = {
|
||||
abs: {
|
||||
signature: "abs(number)",
|
||||
description: "Absolute value. Returns null for non-numbers."
|
||||
},
|
||||
apply: {
|
||||
signature: "apply(function, array)",
|
||||
description: "Execute the function, passing array elements as input values."
|
||||
},
|
||||
array: {
|
||||
signature: "array(value, ...)",
|
||||
description: "Create arrays. Polymorphic: array(number) creates sized array, array(array) copies, array(array, fn) maps, array(text) splits into characters, array(text, sep) splits by separator."
|
||||
},
|
||||
ceiling: {
|
||||
signature: "ceiling(number, place)",
|
||||
description: "Round up. If place is 0 or null, round to smallest integer >= number."
|
||||
},
|
||||
character: {
|
||||
signature: "character(value)",
|
||||
description: "If text, returns the first character. If a non-negative integer, returns the character from that codepoint."
|
||||
},
|
||||
codepoint: {
|
||||
signature: "codepoint(text)",
|
||||
description: "Returns the codepoint number of the first character."
|
||||
},
|
||||
ends_with: {
|
||||
signature: "ends_with(text, suffix)",
|
||||
description: "Returns true if the text ends with the given suffix."
|
||||
},
|
||||
every: {
|
||||
signature: "every(array, function)",
|
||||
description: "Returns true if every element satisfies the predicate."
|
||||
},
|
||||
extract: {
|
||||
signature: "extract(text, pattern, from, to)",
|
||||
description: "Match text to pattern. Returns a record of saved fields, or null if no match."
|
||||
},
|
||||
fallback: {
|
||||
signature: "fallback(requestor_array)",
|
||||
description: "Returns a requestor that tries each requestor in order until one succeeds."
|
||||
},
|
||||
filter: {
|
||||
signature: "filter(array, function)",
|
||||
description: "Returns a new array containing elements for which function returns true."
|
||||
},
|
||||
find: {
|
||||
signature: "find(array, function, reverse, from)",
|
||||
description: "Returns the element number where function returns true, or null if not found. If second arg is not a function, compares directly."
|
||||
},
|
||||
floor: {
|
||||
signature: "floor(number, place)",
|
||||
description: "Round down. If place is 0 or null, round to greatest integer <= number."
|
||||
},
|
||||
format: {
|
||||
signature: "format(text, collection, transformer)",
|
||||
description: "Substitute {key} placeholders in text with values from a collection (array or record)."
|
||||
},
|
||||
fraction: {
|
||||
signature: "fraction(number)",
|
||||
description: "Returns the fractional part of a number."
|
||||
},
|
||||
is_array: {
|
||||
signature: "is_array(value)",
|
||||
description: "Returns true if the value is an array."
|
||||
},
|
||||
is_blob: {
|
||||
signature: "is_blob(value)",
|
||||
description: "Returns true if the value is a blob."
|
||||
},
|
||||
is_character: {
|
||||
signature: "is_character(value)",
|
||||
description: "Returns true if the value is a single character."
|
||||
},
|
||||
is_data: {
|
||||
signature: "is_data(value)",
|
||||
description: "Returns true if the value is data (not a function)."
|
||||
},
|
||||
is_digit: {
|
||||
signature: "is_digit(value)",
|
||||
description: "Returns true if the value is a digit character."
|
||||
},
|
||||
is_false: {
|
||||
signature: "is_false(value)",
|
||||
description: "Returns true if the value is false."
|
||||
},
|
||||
is_fit: {
|
||||
signature: "is_fit(value)",
|
||||
description: "Returns true if the value is a fit integer."
|
||||
},
|
||||
is_function: {
|
||||
signature: "is_function(value)",
|
||||
description: "Returns true if the value is a function."
|
||||
},
|
||||
is_integer: {
|
||||
signature: "is_integer(value)",
|
||||
description: "Returns true if the value is an integer."
|
||||
},
|
||||
is_letter: {
|
||||
signature: "is_letter(value)",
|
||||
description: "Returns true if the value is a letter character."
|
||||
},
|
||||
is_logical: {
|
||||
signature: "is_logical(value)",
|
||||
description: "Returns true if the value is a logical (boolean)."
|
||||
},
|
||||
is_lower: {
|
||||
signature: "is_lower(value)",
|
||||
description: "Returns true if the value is a lowercase character."
|
||||
},
|
||||
is_null: {
|
||||
signature: "is_null(value)",
|
||||
description: "Returns true if the value is null."
|
||||
},
|
||||
is_number: {
|
||||
signature: "is_number(value)",
|
||||
description: "Returns true if the value is a number."
|
||||
},
|
||||
is_object: {
|
||||
signature: "is_object(value)",
|
||||
description: "Returns true if the value is an object (record)."
|
||||
},
|
||||
is_pattern: {
|
||||
signature: "is_pattern(value)",
|
||||
description: "Returns true if the value is a pattern (regex)."
|
||||
},
|
||||
is_stone: {
|
||||
signature: "is_stone(value)",
|
||||
description: "Returns true if the value is frozen (stoned)."
|
||||
},
|
||||
is_text: {
|
||||
signature: "is_text(value)",
|
||||
description: "Returns true if the value is text."
|
||||
},
|
||||
is_true: {
|
||||
signature: "is_true(value)",
|
||||
description: "Returns true if the value is true."
|
||||
},
|
||||
is_upper: {
|
||||
signature: "is_upper(value)",
|
||||
description: "Returns true if the value is an uppercase character."
|
||||
},
|
||||
is_whitespace: {
|
||||
signature: "is_whitespace(value)",
|
||||
description: "Returns true if the value is a whitespace character."
|
||||
},
|
||||
length: {
|
||||
signature: "length(value)",
|
||||
description: "Array: number of elements. Text: number of codepoints. Function: arity. Blob: number of bits. Record: record.length()."
|
||||
},
|
||||
logical: {
|
||||
signature: "logical(value)",
|
||||
description: "Convert to logical. 0/false/null/\"false\" produce false; 1/true/\"true\" produce true."
|
||||
},
|
||||
lower: {
|
||||
signature: "lower(text)",
|
||||
description: "Returns text with all uppercase characters converted to lowercase."
|
||||
},
|
||||
max: {
|
||||
signature: "max(number, number)",
|
||||
description: "Returns the larger of two numbers."
|
||||
},
|
||||
min: {
|
||||
signature: "min(number, number)",
|
||||
description: "Returns the smaller of two numbers."
|
||||
},
|
||||
modulo: {
|
||||
signature: "modulo(dividend, divisor)",
|
||||
description: "Result has the sign of the divisor."
|
||||
},
|
||||
neg: {
|
||||
signature: "neg(number)",
|
||||
description: "Negate. Reverse the sign of a number."
|
||||
},
|
||||
normalize: {
|
||||
signature: "normalize(text)",
|
||||
description: "Unicode normalize."
|
||||
},
|
||||
not: {
|
||||
signature: "not(logical)",
|
||||
description: "Returns the opposite logical. Returns null for non-logicals."
|
||||
},
|
||||
number: {
|
||||
signature: "number(value, radix_or_format)",
|
||||
description: "Convert to number. Polymorphic: number(logical), number(text), number(text, radix), number(text, format)."
|
||||
},
|
||||
parallel: {
|
||||
signature: "parallel(requestor_array, throttle, need)",
|
||||
description: "Start all requestors concurrently. Optional throttle limits concurrency; optional need specifies minimum successes."
|
||||
},
|
||||
print: {
|
||||
signature: "print(value)",
|
||||
description: "Print a value to standard output."
|
||||
},
|
||||
race: {
|
||||
signature: "race(requestor_array, throttle, need)",
|
||||
description: "Like parallel but returns as soon as needed results are obtained. Default need is 1."
|
||||
},
|
||||
record: {
|
||||
signature: "record(value, ...)",
|
||||
description: "Create records. Polymorphic: record(record) copies, record(record, record) merges, record(array) creates from keys."
|
||||
},
|
||||
reduce: {
|
||||
signature: "reduce(array, function, initial, reverse)",
|
||||
description: "Reduce an array to a single value by applying a function to pairs of elements."
|
||||
},
|
||||
remainder: {
|
||||
signature: "remainder(dividend, divisor)",
|
||||
description: "For fit integers: dividend - ((dividend // divisor) * divisor)."
|
||||
},
|
||||
replace: {
|
||||
signature: "replace(text, target, replacement, limit)",
|
||||
description: "Return text with target replaced. Target can be text or pattern. Replacement can be text or function."
|
||||
},
|
||||
reverse: {
|
||||
signature: "reverse(array)",
|
||||
description: "Returns a new array with elements in the opposite order."
|
||||
},
|
||||
round: {
|
||||
signature: "round(number, place)",
|
||||
description: "Round to nearest."
|
||||
},
|
||||
search: {
|
||||
signature: "search(text, target, from)",
|
||||
description: "Search text for target. Returns character position or null."
|
||||
},
|
||||
sequence: {
|
||||
signature: "sequence(requestor_array)",
|
||||
description: "Process requestors in order. Each result becomes input to the next."
|
||||
},
|
||||
sign: {
|
||||
signature: "sign(number)",
|
||||
description: "Returns -1, 0, or 1."
|
||||
},
|
||||
some: {
|
||||
signature: "some(array, function)",
|
||||
description: "Returns true if any element satisfies the predicate."
|
||||
},
|
||||
sort: {
|
||||
signature: "sort(array, select)",
|
||||
description: "Returns a new sorted array. Sort keys must be all numbers or all texts. Ascending and stable."
|
||||
},
|
||||
starts_with: {
|
||||
signature: "starts_with(text, prefix)",
|
||||
description: "Returns true if the text starts with the given prefix."
|
||||
},
|
||||
stone: {
|
||||
signature: "stone(value)",
|
||||
description: "Petrify the value, making it permanently immutable. Deep freeze."
|
||||
},
|
||||
text: {
|
||||
signature: "text(value, ...)",
|
||||
description: "Convert to text. Polymorphic: text(array, sep) joins, text(number, radix/format) formats, text(text, from, to) substrings."
|
||||
},
|
||||
trim: {
|
||||
signature: "trim(text, reject)",
|
||||
description: "Remove characters from both ends. Default removes whitespace."
|
||||
},
|
||||
trunc: {
|
||||
signature: "trunc(number, place)",
|
||||
description: "Truncate toward zero."
|
||||
},
|
||||
upper: {
|
||||
signature: "upper(text)",
|
||||
description: "Returns text with all lowercase characters converted to uppercase."
|
||||
},
|
||||
whole: {
|
||||
signature: "whole(number)",
|
||||
description: "Returns the whole part of a number."
|
||||
},
|
||||
meme: {
|
||||
signature: "meme()",
|
||||
description: "Create a new meme (prototype chain marker)."
|
||||
},
|
||||
proto: {
|
||||
signature: "proto(object, meme)",
|
||||
description: "Set the prototype meme of an object."
|
||||
},
|
||||
isa: {
|
||||
signature: "isa(object, meme)",
|
||||
description: "Returns true if the object has the given meme in its prototype chain."
|
||||
},
|
||||
splat: {
|
||||
signature: "splat(function, array)",
|
||||
description: "Call function with array elements as separate arguments."
|
||||
},
|
||||
use: {
|
||||
signature: "use(path)",
|
||||
description: "Load a module. Returns the module's exported value. Modules are cached and frozen."
|
||||
},
|
||||
pi: {
|
||||
signature: "pi",
|
||||
description: "An approximation of circumference / diameter: 3.1415926535897932."
|
||||
}
|
||||
}
|
||||
|
||||
// Actor intrinsic documentation
|
||||
def actor_docs = {
|
||||
"$me": {
|
||||
signature: "$me",
|
||||
description: "The address of this actor."
|
||||
},
|
||||
"$send": {
|
||||
signature: "$send(address, message)",
|
||||
description: "Send a message to another actor."
|
||||
},
|
||||
"$start": {
|
||||
signature: "$start(script, env)",
|
||||
description: "Start a new actor from a script path."
|
||||
},
|
||||
"$stop": {
|
||||
signature: "$stop()",
|
||||
description: "Stop this actor."
|
||||
},
|
||||
"$delay": {
|
||||
signature: "$delay(milliseconds)",
|
||||
description: "Delay processing for a number of milliseconds."
|
||||
},
|
||||
"$receiver": {
|
||||
signature: "$receiver(function)",
|
||||
description: "Set the message receiver function for this actor."
|
||||
},
|
||||
"$clock": {
|
||||
signature: "$clock(interval, message)",
|
||||
description: "Send a message to self at regular intervals."
|
||||
},
|
||||
"$portal": {
|
||||
signature: "$portal(name)",
|
||||
description: "Create a named portal for inter-actor communication."
|
||||
},
|
||||
"$contact": {
|
||||
signature: "$contact(portal_name)",
|
||||
description: "Connect to a named portal."
|
||||
},
|
||||
"$couple": {
|
||||
signature: "$couple(address)",
|
||||
description: "Couple with another actor for lifecycle management."
|
||||
},
|
||||
"$unneeded": {
|
||||
signature: "$unneeded(function)",
|
||||
description: "Set a function to be called when this actor is no longer needed."
|
||||
},
|
||||
"$connection": {
|
||||
signature: "$connection(address)",
|
||||
description: "Establish a connection with another actor."
|
||||
},
|
||||
"$time_limit": {
|
||||
signature: "$time_limit(milliseconds)",
|
||||
description: "Set a time limit for this actor's execution."
|
||||
}
|
||||
}
|
||||
|
||||
// Provide hover info for a token.
|
||||
var hover = function(doc, line, col, token_at) {
|
||||
var tok = token_at(doc, line, col)
|
||||
var info = null
|
||||
var name = null
|
||||
var _i = 0
|
||||
var _j = 0
|
||||
var scope = null
|
||||
var v = null
|
||||
|
||||
if (tok == null) {
|
||||
return null
|
||||
}
|
||||
|
||||
// Check intrinsic functions
|
||||
if (tok.kind == "name" && tok.value != null) {
|
||||
name = tok.value
|
||||
info = intrinsic_docs[name]
|
||||
if (info != null) {
|
||||
return {
|
||||
contents: {
|
||||
kind: "markdown",
|
||||
value: `**${info.signature}**\n\n${info.description}`
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check actor intrinsics ($name)
|
||||
if (tok.value != null && starts_with(tok.value, "$")) {
|
||||
info = actor_docs[tok.value]
|
||||
if (info != null) {
|
||||
return {
|
||||
contents: {
|
||||
kind: "markdown",
|
||||
value: `**${info.signature}**\n\n${info.description}`
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check keywords
|
||||
if (tok.kind == "var" || tok.kind == "def") {
|
||||
return {
|
||||
contents: {
|
||||
kind: "markdown",
|
||||
value: (tok.kind == "var")
|
||||
? "**var** — Declare a mutable variable."
|
||||
: "**def** — Declare a constant."
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (tok.kind == "disrupt") {
|
||||
return {
|
||||
contents: {
|
||||
kind: "markdown",
|
||||
value: "**disrupt** — Raise an error. Use with **disruption** block to handle errors."
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (tok.kind == "disruption") {
|
||||
return {
|
||||
contents: {
|
||||
kind: "markdown",
|
||||
value: "**disruption** — Error handling block. Catches errors raised by **disrupt**."
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// User variable: show declaration info from scope
|
||||
if (tok.kind == "name" && tok.value != null && doc.ast != null && doc.ast.scopes != null) {
|
||||
_i = 0
|
||||
while (_i < length(doc.ast.scopes)) {
|
||||
scope = doc.ast.scopes[_i]
|
||||
if (scope.vars != null) {
|
||||
_j = 0
|
||||
while (_j < length(scope.vars)) {
|
||||
v = scope.vars[_j]
|
||||
if (v.name == tok.value) {
|
||||
return {
|
||||
contents: {
|
||||
kind: "markdown",
|
||||
value: (v.is_const == true)
|
||||
? `**def** ${v.name}`
|
||||
: `**var** ${v.name}`
|
||||
}
|
||||
}
|
||||
}
|
||||
_j = _j + 1
|
||||
}
|
||||
}
|
||||
_i = _i + 1
|
||||
}
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
return {
|
||||
hover: hover,
|
||||
intrinsic_docs: intrinsic_docs,
|
||||
actor_docs: actor_docs
|
||||
}
|
||||
209
editors/vscode/lsp/lsp.ce
Normal file
209
editors/vscode/lsp/lsp.ce
Normal file
@@ -0,0 +1,209 @@
|
||||
// ƿit Language Server Protocol (LSP) main loop.
|
||||
// Communicates via JSON-RPC over stdin/stdout.
|
||||
|
||||
var fd = use('fd')
|
||||
var json_mod = use('json')
|
||||
var protocol = use('protocol')
|
||||
var analysis_make = use('analysis')
|
||||
var completions = use('completions')
|
||||
var hover_mod = use('hover')
|
||||
var symbols = use('symbols')
|
||||
|
||||
// Get tokenize_mod and parse_mod from the environment.
|
||||
// These are the same functions the compiler uses internally.
|
||||
var tokenize_mod = use('tokenize')
|
||||
var parse_mod = use('parse')
|
||||
|
||||
// Create analysis module bound to tokenize/parse
|
||||
var analysis = analysis_make(tokenize_mod, parse_mod)
|
||||
|
||||
// Document store: URI -> {text, version, ast, tokens, errors}
|
||||
var docs = {}
|
||||
|
||||
// Log to stderr for debugging (does not interfere with protocol).
|
||||
var log = function(msg) {
|
||||
fd.write(2, `[pit-lsp] ${msg}\n`)
|
||||
}
|
||||
|
||||
// Publish diagnostics for a document.
|
||||
var publish_diagnostics = function(uri, doc) {
|
||||
var diags = analysis.diagnostics(doc)
|
||||
protocol.notify("textDocument/publishDiagnostics", {
|
||||
uri: uri,
|
||||
diagnostics: diags
|
||||
})
|
||||
}
|
||||
|
||||
// Parse a document and publish diagnostics.
|
||||
var parse_and_notify = function(uri, src, version) {
|
||||
var doc = analysis.update(docs, uri, {src: src, version: version})
|
||||
publish_diagnostics(uri, doc)
|
||||
}
|
||||
|
||||
// Handle initialize request.
|
||||
var handle_initialize = function(id, params) {
|
||||
protocol.respond(id, {
|
||||
capabilities: {
|
||||
textDocumentSync: {
|
||||
openClose: true,
|
||||
change: 1,
|
||||
save: {includeText: true}
|
||||
},
|
||||
completionProvider: {
|
||||
triggerCharacters: [".", "$"]
|
||||
},
|
||||
hoverProvider: true,
|
||||
definitionProvider: true,
|
||||
documentSymbolProvider: true
|
||||
},
|
||||
serverInfo: {
|
||||
name: "pit-lsp",
|
||||
version: "0.1.0"
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Handle textDocument/didOpen notification.
|
||||
var handle_did_open = function(params) {
|
||||
var td = params.textDocument
|
||||
parse_and_notify(td.uri, td.text, td.version)
|
||||
}
|
||||
|
||||
// Handle textDocument/didChange notification (full text sync).
|
||||
var handle_did_change = function(params) {
|
||||
var td = params.textDocument
|
||||
var changes = params.contentChanges
|
||||
if (length(changes) > 0) {
|
||||
parse_and_notify(td.uri, changes[0].text, td.version)
|
||||
}
|
||||
}
|
||||
|
||||
// Handle textDocument/didClose notification.
|
||||
var handle_did_close = function(params) {
|
||||
var uri = params.textDocument.uri
|
||||
analysis.remove(docs, uri)
|
||||
// Clear diagnostics
|
||||
protocol.notify("textDocument/publishDiagnostics", {
|
||||
uri: uri,
|
||||
diagnostics: []
|
||||
})
|
||||
}
|
||||
|
||||
// Handle textDocument/didSave notification.
|
||||
var handle_did_save = function(params) {
|
||||
var td = params.textDocument
|
||||
if (params.text != null) {
|
||||
parse_and_notify(td.uri, params.text, td.version)
|
||||
}
|
||||
}
|
||||
|
||||
// Handle textDocument/completion request.
|
||||
var handle_completion = function(id, params) {
|
||||
var uri = params.textDocument.uri
|
||||
var pos = params.position
|
||||
var doc = docs[uri]
|
||||
var items = []
|
||||
if (doc != null) {
|
||||
items = completions.complete(doc, pos.line, pos.character)
|
||||
}
|
||||
protocol.respond(id, items)
|
||||
}
|
||||
|
||||
// Handle textDocument/hover request.
|
||||
var handle_hover = function(id, params) {
|
||||
var uri = params.textDocument.uri
|
||||
var pos = params.position
|
||||
var doc = docs[uri]
|
||||
var result = null
|
||||
if (doc != null) {
|
||||
result = hover_mod.hover(doc, pos.line, pos.character, analysis.token_at)
|
||||
}
|
||||
protocol.respond(id, result)
|
||||
}
|
||||
|
||||
// Handle textDocument/definition request.
|
||||
var handle_definition = function(id, params) {
|
||||
var uri = params.textDocument.uri
|
||||
var pos = params.position
|
||||
var doc = docs[uri]
|
||||
var result = null
|
||||
if (doc != null) {
|
||||
result = symbols.definition(doc, pos.line, pos.character, analysis.token_at)
|
||||
}
|
||||
protocol.respond(id, result)
|
||||
}
|
||||
|
||||
// Handle textDocument/documentSymbol request.
|
||||
var handle_document_symbol = function(id, params) {
|
||||
var uri = params.textDocument.uri
|
||||
var doc = docs[uri]
|
||||
var result = []
|
||||
if (doc != null) {
|
||||
result = symbols.document_symbols(doc)
|
||||
}
|
||||
protocol.respond(id, result)
|
||||
}
|
||||
|
||||
// Dispatch a single message. Wrapped in a function for disruption handling.
|
||||
var dispatch_message = function(msg) {
|
||||
var method = msg.method
|
||||
if (method == "initialize") {
|
||||
handle_initialize(msg.id, msg.params)
|
||||
} else if (method == "initialized") {
|
||||
// no-op
|
||||
} else if (method == "textDocument/didOpen") {
|
||||
handle_did_open(msg.params)
|
||||
} else if (method == "textDocument/didChange") {
|
||||
handle_did_change(msg.params)
|
||||
} else if (method == "textDocument/didClose") {
|
||||
handle_did_close(msg.params)
|
||||
} else if (method == "textDocument/didSave") {
|
||||
handle_did_save(msg.params)
|
||||
} else if (method == "textDocument/completion") {
|
||||
handle_completion(msg.id, msg.params)
|
||||
} else if (method == "textDocument/hover") {
|
||||
handle_hover(msg.id, msg.params)
|
||||
} else if (method == "textDocument/definition") {
|
||||
handle_definition(msg.id, msg.params)
|
||||
} else if (method == "textDocument/documentSymbol") {
|
||||
handle_document_symbol(msg.id, msg.params)
|
||||
} else if (method == "shutdown") {
|
||||
protocol.respond(msg.id, null)
|
||||
return "shutdown"
|
||||
} else if (method == "exit") {
|
||||
return "exit"
|
||||
} else {
|
||||
if (msg.id != null) {
|
||||
protocol.respond_error(msg.id, -32601, `Method not found: ${method}`)
|
||||
}
|
||||
}
|
||||
return null
|
||||
} disruption {
|
||||
log(`error handling ${msg.method}`)
|
||||
if (msg.id != null) {
|
||||
protocol.respond_error(msg.id, -32603, `Internal error handling ${msg.method}`)
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
// Main loop.
|
||||
log("starting")
|
||||
|
||||
var running = true
|
||||
var msg = null
|
||||
var result = null
|
||||
|
||||
while (running) {
|
||||
msg = protocol.read_message()
|
||||
if (msg == null) {
|
||||
running = false
|
||||
break
|
||||
}
|
||||
|
||||
result = dispatch_message(msg)
|
||||
if (result == "exit") {
|
||||
running = false
|
||||
}
|
||||
}
|
||||
|
||||
log("stopped")
|
||||
102
editors/vscode/lsp/protocol.cm
Normal file
102
editors/vscode/lsp/protocol.cm
Normal file
@@ -0,0 +1,102 @@
|
||||
// JSON-RPC protocol helpers for LSP communication over stdin/stdout.
|
||||
// Reads Content-Length framed messages from stdin, writes to stdout.
|
||||
|
||||
var fd = use('fd')
|
||||
var json = use('json')
|
||||
|
||||
// Read a single JSON-RPC message from stdin.
|
||||
// Protocol: "Content-Length: N\r\n\r\n" followed by N bytes of JSON.
|
||||
var read_message = function() {
|
||||
var header = ""
|
||||
var ch = null
|
||||
var content_length = null
|
||||
var body = null
|
||||
var total = 0
|
||||
var chunk = null
|
||||
|
||||
// Read header byte by byte until we hit \r\n\r\n
|
||||
while (true) {
|
||||
ch = fd.read(0, 1)
|
||||
if (ch == null) {
|
||||
return null
|
||||
}
|
||||
header = header + text(ch)
|
||||
if (ends_with(header, "\r\n\r\n")) {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// Parse Content-Length from header
|
||||
var lines = array(header, "\r\n")
|
||||
var _i = 0
|
||||
while (_i < length(lines)) {
|
||||
if (starts_with(lines[_i], "Content-Length:")) {
|
||||
content_length = number(trim(text(lines[_i], 16)))
|
||||
}
|
||||
_i = _i + 1
|
||||
}
|
||||
|
||||
if (content_length == null) {
|
||||
return null
|
||||
}
|
||||
|
||||
// Read exactly content_length bytes
|
||||
body = ""
|
||||
total = 0
|
||||
while (total < content_length) {
|
||||
chunk = fd.read(0, content_length - total)
|
||||
if (chunk == null) {
|
||||
return null
|
||||
}
|
||||
chunk = text(chunk)
|
||||
body = body + chunk
|
||||
total = total + length(chunk)
|
||||
}
|
||||
|
||||
return json.decode(body)
|
||||
}
|
||||
|
||||
// Send a JSON-RPC message to stdout.
|
||||
var send_message = function(msg) {
|
||||
var body = json.encode(msg)
|
||||
var header = `Content-Length: ${text(length(body))}\r\n\r\n`
|
||||
fd.write(1, header + body)
|
||||
}
|
||||
|
||||
// Send a JSON-RPC response for a request.
|
||||
var respond = function(id, result) {
|
||||
send_message({
|
||||
jsonrpc: "2.0",
|
||||
id: id,
|
||||
result: result
|
||||
})
|
||||
}
|
||||
|
||||
// Send a JSON-RPC error response.
|
||||
var respond_error = function(id, code, message) {
|
||||
send_message({
|
||||
jsonrpc: "2.0",
|
||||
id: id,
|
||||
error: {
|
||||
code: code,
|
||||
message: message
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Send a JSON-RPC notification (no id).
|
||||
var notify = function(method, params) {
|
||||
send_message({
|
||||
jsonrpc: "2.0",
|
||||
method: method,
|
||||
params: params
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
read_message: read_message,
|
||||
send_message: send_message,
|
||||
respond: respond,
|
||||
respond_error: respond_error,
|
||||
notify: notify
|
||||
}
|
||||
238
editors/vscode/lsp/symbols.cm
Normal file
238
editors/vscode/lsp/symbols.cm
Normal file
@@ -0,0 +1,238 @@
|
||||
// Document symbols and go-to-definition provider for the ƿit LSP.
|
||||
|
||||
// SymbolKind constants (LSP spec)
|
||||
def KIND_FUNCTION = 12
|
||||
def KIND_VARIABLE = 13
|
||||
def KIND_CONSTANT = 14
|
||||
|
||||
// Walk AST to extract document symbols (top-level vars/defs and functions).
|
||||
var document_symbols = function(doc) {
|
||||
var symbols = []
|
||||
var ast = doc.ast
|
||||
var _i = 0
|
||||
var _j = 0
|
||||
var stmt = null
|
||||
var decl = null
|
||||
var name = null
|
||||
var kind = null
|
||||
var range = null
|
||||
|
||||
if (ast == null || ast.statements == null) {
|
||||
return symbols
|
||||
}
|
||||
|
||||
while (_i < length(ast.statements)) {
|
||||
stmt = ast.statements[_i]
|
||||
|
||||
if (stmt.kind == "var" || stmt.kind == "def") {
|
||||
name = null
|
||||
kind = KIND_VARIABLE
|
||||
|
||||
if (stmt.left != null && stmt.left.name != null) {
|
||||
name = stmt.left.name
|
||||
}
|
||||
|
||||
if (stmt.kind == "def") {
|
||||
kind = KIND_CONSTANT
|
||||
}
|
||||
|
||||
if (stmt.right != null && (stmt.right.kind == "function" || stmt.right.kind == "arrow function")) {
|
||||
kind = KIND_FUNCTION
|
||||
}
|
||||
|
||||
if (name != null) {
|
||||
range = {
|
||||
start: {line: stmt.from_row, character: stmt.from_column},
|
||||
end: {line: stmt.to_row, character: stmt.to_column}
|
||||
}
|
||||
symbols[] = {
|
||||
name: name,
|
||||
kind: kind,
|
||||
range: range,
|
||||
selectionRange: {
|
||||
start: {line: stmt.left.from_row, character: stmt.left.from_column},
|
||||
end: {line: stmt.left.to_row, character: stmt.left.to_column}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (stmt.kind == "var_list" && stmt.list != null) {
|
||||
_j = 0
|
||||
while (_j < length(stmt.list)) {
|
||||
decl = stmt.list[_j]
|
||||
if (decl.left != null && decl.left.name != null) {
|
||||
kind = (decl.kind == "def") ? KIND_CONSTANT : KIND_VARIABLE
|
||||
if (decl.right != null && (decl.right.kind == "function" || decl.right.kind == "arrow function")) {
|
||||
kind = KIND_FUNCTION
|
||||
}
|
||||
range = {
|
||||
start: {line: decl.from_row, character: decl.from_column},
|
||||
end: {line: decl.to_row, character: decl.to_column}
|
||||
}
|
||||
symbols[] = {
|
||||
name: decl.left.name,
|
||||
kind: kind,
|
||||
range: range,
|
||||
selectionRange: {
|
||||
start: {line: decl.left.from_row, character: decl.left.from_column},
|
||||
end: {line: decl.left.to_row, character: decl.left.to_column}
|
||||
}
|
||||
}
|
||||
}
|
||||
_j = _j + 1
|
||||
}
|
||||
}
|
||||
|
||||
_i = _i + 1
|
||||
}
|
||||
|
||||
return symbols
|
||||
}
|
||||
|
||||
// Find the declaration location of a name at a given position.
|
||||
var definition = function(doc, line, col, token_at) {
|
||||
var tok = token_at(doc, line, col)
|
||||
var ast = doc.ast
|
||||
var name = null
|
||||
var _i = 0
|
||||
var _j = 0
|
||||
var scope = null
|
||||
var v = null
|
||||
var decl = null
|
||||
|
||||
if (tok == null || tok.kind != "name" || tok.value == null) {
|
||||
return null
|
||||
}
|
||||
|
||||
name = tok.value
|
||||
|
||||
if (ast == null) {
|
||||
return null
|
||||
}
|
||||
|
||||
// Search through scopes for the variable declaration
|
||||
if (ast.scopes != null) {
|
||||
_i = 0
|
||||
while (_i < length(ast.scopes)) {
|
||||
scope = ast.scopes[_i]
|
||||
if (scope.vars != null) {
|
||||
_j = 0
|
||||
while (_j < length(scope.vars)) {
|
||||
v = scope.vars[_j]
|
||||
if (v.name == name) {
|
||||
decl = find_declaration(ast.statements, name)
|
||||
if (decl != null) {
|
||||
return {
|
||||
uri: doc.uri,
|
||||
range: {
|
||||
start: {line: decl.from_row, character: decl.from_column},
|
||||
end: {line: decl.to_row, character: decl.to_column}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_j = _j + 1
|
||||
}
|
||||
}
|
||||
_i = _i + 1
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback: walk statements for var/def with this name
|
||||
decl = find_declaration(ast.statements, name)
|
||||
if (decl != null) {
|
||||
return {
|
||||
uri: doc.uri,
|
||||
range: {
|
||||
start: {line: decl.from_row, character: decl.from_column},
|
||||
end: {line: decl.to_row, character: decl.to_column}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
// Recursively search statements for a var/def declaration of a given name.
|
||||
var find_declaration = function(statements, name) {
|
||||
var _i = 0
|
||||
var _j = 0
|
||||
var stmt = null
|
||||
var result = null
|
||||
|
||||
if (statements == null) {
|
||||
return null
|
||||
}
|
||||
|
||||
while (_i < length(statements)) {
|
||||
stmt = statements[_i]
|
||||
|
||||
// Direct var/def
|
||||
if ((stmt.kind == "var" || stmt.kind == "def")
|
||||
&& stmt.left != null && stmt.left.name == name) {
|
||||
return stmt
|
||||
}
|
||||
|
||||
// var_list
|
||||
if (stmt.kind == "var_list" && stmt.list != null) {
|
||||
_j = 0
|
||||
while (_j < length(stmt.list)) {
|
||||
if (stmt.list[_j].left != null && stmt.list[_j].left.name == name) {
|
||||
return stmt.list[_j]
|
||||
}
|
||||
_j = _j + 1
|
||||
}
|
||||
}
|
||||
|
||||
// Recurse into blocks
|
||||
if (stmt.statements != null) {
|
||||
result = find_declaration(stmt.statements, name)
|
||||
if (result != null) {
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
// if/else
|
||||
if (stmt.kind == "if") {
|
||||
if (stmt.then != null && stmt.then.statements != null) {
|
||||
result = find_declaration(stmt.then.statements, name)
|
||||
if (result != null) {
|
||||
return result
|
||||
}
|
||||
}
|
||||
if (stmt.else != null && stmt.else.statements != null) {
|
||||
result = find_declaration(stmt.else.statements, name)
|
||||
if (result != null) {
|
||||
return result
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Function body
|
||||
if ((stmt.kind == "function" || stmt.kind == "arrow function") && stmt.statements != null) {
|
||||
result = find_declaration(stmt.statements, name)
|
||||
if (result != null) {
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
// var/def with function right side
|
||||
if ((stmt.kind == "var" || stmt.kind == "def") && stmt.right != null) {
|
||||
if ((stmt.right.kind == "function" || stmt.right.kind == "arrow function") && stmt.right.statements != null) {
|
||||
result = find_declaration(stmt.right.statements, name)
|
||||
if (result != null) {
|
||||
return result
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_i = _i + 1
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
return {
|
||||
document_symbols: document_symbols,
|
||||
definition: definition
|
||||
}
|
||||
62
editors/vscode/package.json
Normal file
62
editors/vscode/package.json
Normal file
@@ -0,0 +1,62 @@
|
||||
{
|
||||
"name": "pit-language",
|
||||
"displayName": "ƿit Language",
|
||||
"description": "Language support for ƿit (.ce/.cm) — syntax highlighting, diagnostics, completions, hover, and go-to-definition",
|
||||
"version": "0.1.0",
|
||||
"publisher": "pit-lang",
|
||||
"engines": {
|
||||
"vscode": "^1.75.0"
|
||||
},
|
||||
"categories": [
|
||||
"Programming Languages"
|
||||
],
|
||||
"activationEvents": [
|
||||
"onLanguage:pit"
|
||||
],
|
||||
"main": "./out/extension.js",
|
||||
"contributes": {
|
||||
"languages": [
|
||||
{
|
||||
"id": "pit",
|
||||
"aliases": [
|
||||
"ƿit",
|
||||
"pit"
|
||||
],
|
||||
"extensions": [
|
||||
".ce",
|
||||
".cm"
|
||||
],
|
||||
"configuration": "./language-configuration.json"
|
||||
}
|
||||
],
|
||||
"grammars": [
|
||||
{
|
||||
"language": "pit",
|
||||
"scopeName": "source.pit",
|
||||
"path": "./syntaxes/pit.tmLanguage.json"
|
||||
}
|
||||
],
|
||||
"configuration": {
|
||||
"title": "ƿit",
|
||||
"properties": {
|
||||
"pit.cellPath": {
|
||||
"type": "string",
|
||||
"default": "cell",
|
||||
"description": "Path to the cell executable"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"compile": "tsc -p ./",
|
||||
"watch": "tsc -watch -p ./"
|
||||
},
|
||||
"dependencies": {
|
||||
"vscode-languageclient": "^9.0.0",
|
||||
"vscode-languageserver-protocol": "^3.17.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/vscode": "^1.75.0",
|
||||
"typescript": "^5.0.0"
|
||||
}
|
||||
}
|
||||
44
editors/vscode/src/extension.ts
Normal file
44
editors/vscode/src/extension.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import * as path from "path";
|
||||
import { workspace, ExtensionContext } from "vscode";
|
||||
import {
|
||||
LanguageClient,
|
||||
LanguageClientOptions,
|
||||
ServerOptions,
|
||||
} from "vscode-languageclient/node";
|
||||
|
||||
let client: LanguageClient;
|
||||
|
||||
export function activate(context: ExtensionContext) {
|
||||
const config = workspace.getConfiguration("pit");
|
||||
const cellPath = config.get<string>("cellPath", "cell");
|
||||
const lspDir = path.join(context.extensionPath, "lsp");
|
||||
|
||||
const serverOptions: ServerOptions = {
|
||||
command: cellPath,
|
||||
args: ["lsp/lsp"],
|
||||
options: { cwd: lspDir },
|
||||
};
|
||||
|
||||
const clientOptions: LanguageClientOptions = {
|
||||
documentSelector: [{ scheme: "file", language: "pit" }],
|
||||
synchronize: {
|
||||
fileEvents: workspace.createFileSystemWatcher("**/*.{ce,cm}"),
|
||||
},
|
||||
};
|
||||
|
||||
client = new LanguageClient(
|
||||
"pitLanguageServer",
|
||||
"ƿit Language Server",
|
||||
serverOptions,
|
||||
clientOptions
|
||||
);
|
||||
|
||||
client.start();
|
||||
}
|
||||
|
||||
export function deactivate(): Thenable<void> | undefined {
|
||||
if (!client) {
|
||||
return undefined;
|
||||
}
|
||||
return client.stop();
|
||||
}
|
||||
160
editors/vscode/syntaxes/pit.tmLanguage.json
Normal file
160
editors/vscode/syntaxes/pit.tmLanguage.json
Normal file
@@ -0,0 +1,160 @@
|
||||
{
|
||||
"$schema": "https://raw.githubusercontent.com/martinring/tmlanguage/master/tmlanguage.json",
|
||||
"name": "pit",
|
||||
"scopeName": "source.pit",
|
||||
"patterns": [
|
||||
{ "include": "#comment-line" },
|
||||
{ "include": "#comment-block" },
|
||||
{ "include": "#string-template" },
|
||||
{ "include": "#string-double" },
|
||||
{ "include": "#regexp" },
|
||||
{ "include": "#keyword-control" },
|
||||
{ "include": "#keyword-error" },
|
||||
{ "include": "#storage-type" },
|
||||
{ "include": "#constant-language" },
|
||||
{ "include": "#variable-language" },
|
||||
{ "include": "#actor-intrinsic" },
|
||||
{ "include": "#keyword-operator" },
|
||||
{ "include": "#arrow-function" },
|
||||
{ "include": "#support-function" },
|
||||
{ "include": "#constant-numeric-hex" },
|
||||
{ "include": "#constant-numeric-binary" },
|
||||
{ "include": "#constant-numeric-octal" },
|
||||
{ "include": "#constant-numeric" },
|
||||
{ "include": "#punctuation" }
|
||||
],
|
||||
"repository": {
|
||||
"comment-line": {
|
||||
"name": "comment.line.double-slash.pit",
|
||||
"match": "//.*$"
|
||||
},
|
||||
"comment-block": {
|
||||
"name": "comment.block.pit",
|
||||
"begin": "/\\*",
|
||||
"end": "\\*/",
|
||||
"beginCaptures": { "0": { "name": "punctuation.definition.comment.begin.pit" } },
|
||||
"endCaptures": { "0": { "name": "punctuation.definition.comment.end.pit" } }
|
||||
},
|
||||
"string-double": {
|
||||
"name": "string.quoted.double.pit",
|
||||
"begin": "\"",
|
||||
"end": "\"",
|
||||
"beginCaptures": { "0": { "name": "punctuation.definition.string.begin.pit" } },
|
||||
"endCaptures": { "0": { "name": "punctuation.definition.string.end.pit" } },
|
||||
"patterns": [
|
||||
{
|
||||
"name": "constant.character.escape.pit",
|
||||
"match": "\\\\(?:[\"\\\\bfnrt/]|u[0-9a-fA-F]{4})"
|
||||
}
|
||||
]
|
||||
},
|
||||
"string-template": {
|
||||
"name": "string.template.pit",
|
||||
"begin": "`",
|
||||
"end": "`",
|
||||
"beginCaptures": { "0": { "name": "punctuation.definition.string.template.begin.pit" } },
|
||||
"endCaptures": { "0": { "name": "punctuation.definition.string.template.end.pit" } },
|
||||
"patterns": [
|
||||
{
|
||||
"name": "constant.character.escape.pit",
|
||||
"match": "\\\\(?:[`\\\\bfnrt/$]|u[0-9a-fA-F]{4})"
|
||||
},
|
||||
{
|
||||
"name": "meta.template.expression.pit",
|
||||
"begin": "\\$\\{",
|
||||
"end": "\\}",
|
||||
"beginCaptures": { "0": { "name": "punctuation.definition.template-expression.begin.pit" } },
|
||||
"endCaptures": { "0": { "name": "punctuation.definition.template-expression.end.pit" } },
|
||||
"patterns": [
|
||||
{ "include": "source.pit" }
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"regexp": {
|
||||
"name": "string.regexp.pit",
|
||||
"begin": "(?<=[=(:,;!&|?~^>]|^|return|disrupt)\\s*(/(?![/*]))",
|
||||
"end": "/([gimsuvy]*)",
|
||||
"beginCaptures": { "1": { "name": "punctuation.definition.string.begin.pit" } },
|
||||
"endCaptures": { "1": { "name": "keyword.other.pit" } },
|
||||
"patterns": [
|
||||
{
|
||||
"name": "constant.character.escape.pit",
|
||||
"match": "\\\\."
|
||||
}
|
||||
]
|
||||
},
|
||||
"keyword-control": {
|
||||
"name": "keyword.control.pit",
|
||||
"match": "\\b(if|else|for|while|do|break|continue|return|go)\\b"
|
||||
},
|
||||
"keyword-error": {
|
||||
"name": "keyword.control.error.pit",
|
||||
"match": "\\b(disrupt|disruption)\\b"
|
||||
},
|
||||
"storage-type": {
|
||||
"patterns": [
|
||||
{
|
||||
"name": "storage.type.pit",
|
||||
"match": "\\b(var|def)\\b"
|
||||
},
|
||||
{
|
||||
"name": "storage.type.function.pit",
|
||||
"match": "\\bfunction\\b"
|
||||
}
|
||||
]
|
||||
},
|
||||
"constant-language": {
|
||||
"name": "constant.language.pit",
|
||||
"match": "\\b(null|true|false)\\b"
|
||||
},
|
||||
"variable-language": {
|
||||
"name": "variable.language.this.pit",
|
||||
"match": "\\bthis\\b"
|
||||
},
|
||||
"actor-intrinsic": {
|
||||
"name": "variable.language.actor.pit",
|
||||
"match": "\\$[a-zA-Z_][a-zA-Z0-9_]*"
|
||||
},
|
||||
"keyword-operator": {
|
||||
"name": "keyword.operator.pit",
|
||||
"match": "\\b(delete|in|typeof)\\b"
|
||||
},
|
||||
"arrow-function": {
|
||||
"name": "storage.type.function.arrow.pit",
|
||||
"match": "=>"
|
||||
},
|
||||
"support-function": {
|
||||
"name": "support.function.pit",
|
||||
"match": "\\b(abs|apply|array|ceiling|character|codepoint|ends_with|every|extract|fallback|filter|find|floor|for|format|fraction|is_array|is_blob|is_character|is_data|is_digit|is_false|is_fit|is_function|is_integer|is_letter|is_logical|is_lower|is_null|is_number|is_object|is_pattern|is_stone|is_text|is_true|is_upper|is_whitespace|length|logical|lower|max|min|modulo|neg|normalize|not|number|parallel|print|race|record|reduce|remainder|replace|reverse|round|search|sequence|sign|some|sort|starts_with|stone|text|trim|trunc|upper|whole|meme|proto|isa|splat|use)(?=\\s*\\()"
|
||||
},
|
||||
"constant-numeric-hex": {
|
||||
"name": "constant.numeric.hex.pit",
|
||||
"match": "\\b0[xX][0-9a-fA-F]+\\b"
|
||||
},
|
||||
"constant-numeric-binary": {
|
||||
"name": "constant.numeric.binary.pit",
|
||||
"match": "\\b0[bB][01]+\\b"
|
||||
},
|
||||
"constant-numeric-octal": {
|
||||
"name": "constant.numeric.octal.pit",
|
||||
"match": "\\b0[oO][0-7]+\\b"
|
||||
},
|
||||
"constant-numeric": {
|
||||
"name": "constant.numeric.pit",
|
||||
"match": "\\b[0-9]+(\\.[0-9]+)?([eE][+-]?[0-9]+)?\\b"
|
||||
},
|
||||
"punctuation": {
|
||||
"patterns": [
|
||||
{
|
||||
"name": "punctuation.separator.comma.pit",
|
||||
"match": ","
|
||||
},
|
||||
{
|
||||
"name": "punctuation.terminator.statement.pit",
|
||||
"match": ";"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
13
editors/vscode/tsconfig.json
Normal file
13
editors/vscode/tsconfig.json
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"target": "ES2020",
|
||||
"outDir": "out",
|
||||
"lib": ["ES2020"],
|
||||
"sourceMap": true,
|
||||
"rootDir": "src",
|
||||
"strict": true
|
||||
},
|
||||
"include": ["src"],
|
||||
"exclude": ["node_modules", "out"]
|
||||
}
|
||||
34
fd.c
34
fd.c
@@ -504,7 +504,7 @@ JSC_SCALL(fd_readdir,
|
||||
ret = JS_NewArray(js);
|
||||
do {
|
||||
if (strcmp(ffd.cFileName, ".") == 0 || strcmp(ffd.cFileName, "..") == 0) continue;
|
||||
JS_ArrayPush(js, ret,JS_NewString(js, ffd.cFileName));
|
||||
JS_ArrayPush(js, &ret, JS_NewString(js, ffd.cFileName));
|
||||
} while (FindNextFile(hFind, &ffd) != 0);
|
||||
FindClose(hFind);
|
||||
}
|
||||
@@ -516,7 +516,7 @@ JSC_SCALL(fd_readdir,
|
||||
ret = JS_NewArray(js);
|
||||
while ((dir = readdir(d)) != NULL) {
|
||||
if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0) continue;
|
||||
JS_ArrayPush(js, ret, JS_NewString(js, dir->d_name));
|
||||
JS_ArrayPush(js, &ret, JS_NewString(js, dir->d_name));
|
||||
}
|
||||
closedir(d);
|
||||
} else {
|
||||
@@ -565,18 +565,22 @@ JSC_CCALL(fd_slurpwrite,
|
||||
if (!str) return JS_EXCEPTION;
|
||||
int fd = open(str, O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
||||
if (fd < 0) {
|
||||
ret = JS_ThrowInternalError(js, "open failed for %s: %s", str, strerror(errno));
|
||||
JS_FreeCString(js, str);
|
||||
return JS_ThrowInternalError(js, "open failed for %s: %s", str, strerror(errno));
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
ssize_t written = write(fd, data, len);
|
||||
close(fd);
|
||||
|
||||
if (written != (ssize_t)len) {
|
||||
ret = JS_ThrowInternalError(js, "write failed for %s: %s", str, strerror(errno));
|
||||
JS_FreeCString(js, str);
|
||||
return ret;
|
||||
}
|
||||
|
||||
JS_FreeCString(js, str);
|
||||
|
||||
if (written != (ssize_t)len)
|
||||
return JS_ThrowInternalError(js, "write failed for %s: %s", str, strerror(errno));
|
||||
|
||||
return JS_NULL;
|
||||
)
|
||||
|
||||
@@ -598,7 +602,7 @@ static void visit_directory(JSContext *js, JSValue results, int *result_count, c
|
||||
} else {
|
||||
strcpy(item_rel, ffd.cFileName);
|
||||
}
|
||||
JS_SetPropertyUint32(js, results, (*result_count)++, JS_NewString(js, item_rel));
|
||||
JS_SetPropertyNumber(js, results, (*result_count)++, JS_NewString(js, item_rel));
|
||||
|
||||
if (recurse) {
|
||||
struct stat st;
|
||||
@@ -623,7 +627,7 @@ static void visit_directory(JSContext *js, JSValue results, int *result_count, c
|
||||
} else {
|
||||
strcpy(item_rel, dir->d_name);
|
||||
}
|
||||
JS_SetPropertyUint32(js, results, (*result_count)++, JS_NewString(js, item_rel));
|
||||
JS_SetPropertyNumber(js, results, (*result_count)++, JS_NewString(js, item_rel));
|
||||
|
||||
if (recurse) {
|
||||
struct stat st;
|
||||
@@ -664,17 +668,21 @@ JSC_CCALL(fd_realpath,
|
||||
#ifdef _WIN32
|
||||
char resolved[PATH_MAX];
|
||||
DWORD len = GetFullPathNameA(path, PATH_MAX, resolved, NULL);
|
||||
JS_FreeCString(js, path);
|
||||
if (len == 0 || len >= PATH_MAX) {
|
||||
return JS_ThrowInternalError(js, "realpath failed for %s: %s", path, strerror(errno));
|
||||
JSValue err = JS_ThrowInternalError(js, "realpath failed for %s: %s", path, strerror(errno));
|
||||
JS_FreeCString(js, path);
|
||||
return err;
|
||||
}
|
||||
JS_FreeCString(js, path);
|
||||
return JS_NewString(js, resolved);
|
||||
#else
|
||||
char *resolved = realpath(path, NULL);
|
||||
JS_FreeCString(js, path);
|
||||
if (!resolved) {
|
||||
return JS_ThrowInternalError(js, "realpath failed for %s: %s", path, strerror(errno));
|
||||
JSValue err = JS_ThrowInternalError(js, "realpath failed for %s: %s", path, strerror(errno));
|
||||
JS_FreeCString(js, path);
|
||||
return err;
|
||||
}
|
||||
JS_FreeCString(js, path);
|
||||
JSValue result = JS_NewString(js, resolved);
|
||||
free(resolved);
|
||||
return result;
|
||||
|
||||
19
fd.cm
19
fd.cm
@@ -1,4 +1,4 @@
|
||||
var fd = this
|
||||
var fd = native
|
||||
var wildstar = use('wildstar')
|
||||
|
||||
function last_pos(str, sep) {
|
||||
@@ -12,11 +12,11 @@ function last_pos(str, sep) {
|
||||
|
||||
// Helper to join paths
|
||||
function join_paths(base, rel) {
|
||||
base = replace(base, /\/+$/, "")
|
||||
rel = replace(rel, /^\/+/, "")
|
||||
if (!base) return rel
|
||||
if (!rel) return base
|
||||
return base + "/" + rel
|
||||
var b = replace(base, /\/+$/, "")
|
||||
var r = replace(rel, /^\/+/, "")
|
||||
if (!b) return r
|
||||
if (!r) return b
|
||||
return b + "/" + r
|
||||
}
|
||||
|
||||
fd.join_paths = join_paths
|
||||
@@ -39,7 +39,8 @@ fd.stem = function stem(path) {
|
||||
}
|
||||
|
||||
fd.globfs = function(globs, dir) {
|
||||
if (dir == null) dir = "."
|
||||
var _dir = dir
|
||||
if (_dir == null) _dir = "."
|
||||
var results = []
|
||||
|
||||
function check_neg(path) {
|
||||
@@ -88,9 +89,9 @@ fd.globfs = function(globs, dir) {
|
||||
});
|
||||
}
|
||||
|
||||
var st = fd.stat(dir)
|
||||
var st = fd.stat(_dir)
|
||||
if (st && st.isDirectory) {
|
||||
visit(dir, "")
|
||||
visit(_dir, "")
|
||||
}
|
||||
|
||||
return results
|
||||
|
||||
@@ -324,7 +324,7 @@ static void listfiles_cb(const char *path, void *userdata) {
|
||||
// Playdate listfiles returns just the name, but sometimes with slash for dir?
|
||||
// Docs say "names of files".
|
||||
|
||||
JS_SetPropertyUint32(ctx->js, ctx->array, ctx->index++, JS_NewString(ctx->js, path));
|
||||
JS_SetPropertyNumber(ctx->js, ctx->array, ctx->index++, JS_NewString(ctx->js, path));
|
||||
}
|
||||
|
||||
JSC_SCALL(fd_readdir,
|
||||
@@ -427,7 +427,7 @@ static void enum_cb(const char *name, void *userdata) {
|
||||
strcpy(item_rel, name);
|
||||
}
|
||||
|
||||
JS_SetPropertyUint32(ctx->js, ctx->results, (*ctx->count)++, JS_NewString(ctx->js, item_rel));
|
||||
JS_SetPropertyNumber(ctx->js, ctx->results, (*ctx->count)++, JS_NewString(ctx->js, item_rel));
|
||||
|
||||
if (ctx->recurse) {
|
||||
// Check if directory
|
||||
|
||||
13
fold.ce
Normal file
13
fold.ce
Normal file
@@ -0,0 +1,13 @@
|
||||
var fd = use("fd")
|
||||
var json = use("json")
|
||||
|
||||
var filename = args[0]
|
||||
var src = text(fd.slurp(filename))
|
||||
var tokenize = use("tokenize")
|
||||
var parse = use("parse")
|
||||
var fold = use("fold")
|
||||
|
||||
var tok_result = tokenize(src, filename)
|
||||
var ast = parse(tok_result.tokens, src, filename, tokenize)
|
||||
var folded = fold(ast)
|
||||
print(json.encode(folded))
|
||||
BIN
fold_new.mach
Normal file
BIN
fold_new.mach
Normal file
Binary file not shown.
283
internal/bootstrap.cm
Normal file
283
internal/bootstrap.cm
Normal file
@@ -0,0 +1,283 @@
|
||||
// Hidden vars come from env:
|
||||
// CLI mode (cell_init): os, args, core_path, shop_path, emit_qbe, dump_mach
|
||||
// Actor spawn (script_startup): os, json, nota, wota, actorsym, init, core_path, shop_path
|
||||
// args[0] = script name, args[1..] = user args
|
||||
var load_internal = os.load_internal
|
||||
function use_embed(name) {
|
||||
return load_internal("js_" + name + "_use")
|
||||
}
|
||||
|
||||
var fd = use_embed('fd')
|
||||
var json = use_embed('json')
|
||||
|
||||
var use_cache = {}
|
||||
use_cache['fd'] = fd
|
||||
use_cache['os'] = os
|
||||
use_cache['json'] = json
|
||||
|
||||
// Bootstrap: load tokenize.cm, parse.cm, fold.cm from pre-compiled mach bytecode
|
||||
function use_basic(path) {
|
||||
if (use_cache[path])
|
||||
return use_cache[path]
|
||||
var result = use_embed(replace(path, '/', '_'))
|
||||
use_cache[path] = result
|
||||
return result
|
||||
}
|
||||
|
||||
// Load a module from .mach bytecode (bootstrap modules have no source fallback)
|
||||
function boot_load(name, env) {
|
||||
var mach_path = core_path + '/' + name + ".mach"
|
||||
var data = null
|
||||
if (fd.is_file(mach_path)) {
|
||||
data = fd.slurp(mach_path)
|
||||
return mach_load(data, env)
|
||||
}
|
||||
print("error: missing bootstrap bytecode: " + mach_path + "\n")
|
||||
disrupt
|
||||
}
|
||||
|
||||
var boot_env = {use: use_basic}
|
||||
var tokenize_mod = boot_load("tokenize", boot_env)
|
||||
var parse_mod = boot_load("parse", boot_env)
|
||||
var fold_mod = boot_load("fold", boot_env)
|
||||
use_cache['tokenize'] = tokenize_mod
|
||||
use_cache['parse'] = parse_mod
|
||||
use_cache['fold'] = fold_mod
|
||||
|
||||
// Always load mcode compiler module
|
||||
var mcode_mod = boot_load("mcode", boot_env)
|
||||
use_cache['mcode'] = mcode_mod
|
||||
var streamline_mod = null
|
||||
var qbe_emit_mod = null
|
||||
|
||||
// Warn if any .cm source is newer than its .mach bytecode
|
||||
function check_mach_stale() {
|
||||
var pairs = [
|
||||
["tokenize.cm", "tokenize.mach"],
|
||||
["parse.cm", "parse.mach"],
|
||||
["fold.cm", "fold.mach"],
|
||||
["mcode.cm", "mcode.mach"],
|
||||
["streamline.cm", "streamline.mach"],
|
||||
["qbe.cm", "qbe.mach"],
|
||||
["qbe_emit.cm", "qbe_emit.mach"],
|
||||
["internal/bootstrap.cm", "internal/bootstrap.mach"],
|
||||
["internal/engine.cm", "internal/engine.mach"]
|
||||
]
|
||||
var stale = []
|
||||
var _i = 0
|
||||
var cm_path = null
|
||||
var mach_path = null
|
||||
var cm_stat = null
|
||||
var mach_stat = null
|
||||
while (_i < length(pairs)) {
|
||||
cm_path = core_path + '/' + pairs[_i][0]
|
||||
mach_path = core_path + '/' + pairs[_i][1]
|
||||
if (fd.is_file(cm_path) && fd.is_file(mach_path)) {
|
||||
cm_stat = fd.stat(cm_path)
|
||||
mach_stat = fd.stat(mach_path)
|
||||
if (cm_stat.mtime > mach_stat.mtime) {
|
||||
push(stale, pairs[_i][0])
|
||||
}
|
||||
}
|
||||
_i = _i + 1
|
||||
}
|
||||
if (length(stale) > 0) {
|
||||
print("warning: bytecode is stale for: " + text(stale, ", ") + "\n")
|
||||
print("run 'make regen' or './cell --core . regen.cm' to update\n")
|
||||
}
|
||||
}
|
||||
check_mach_stale()
|
||||
|
||||
// analyze: tokenize + parse, check for errors
|
||||
function analyze(src, filename) {
|
||||
var tok_result = tokenize_mod(src, filename)
|
||||
var ast = parse_mod(tok_result.tokens, src, filename, tokenize_mod)
|
||||
var _i = 0
|
||||
var prev_line = -1
|
||||
var prev_msg = null
|
||||
var e = null
|
||||
var msg = null
|
||||
var line = null
|
||||
var col = null
|
||||
var has_errors = ast.errors != null && length(ast.errors) > 0
|
||||
if (has_errors) {
|
||||
while (_i < length(ast.errors)) {
|
||||
e = ast.errors[_i]
|
||||
msg = e.message
|
||||
line = e.line
|
||||
col = e.column
|
||||
if (msg != prev_msg || line != prev_line) {
|
||||
if (line != null && col != null) {
|
||||
print(`${filename}:${text(line)}:${text(col)}: error: ${msg}`)
|
||||
} else {
|
||||
print(`${filename}: error: ${msg}`)
|
||||
}
|
||||
}
|
||||
prev_line = line
|
||||
prev_msg = msg
|
||||
_i = _i + 1
|
||||
}
|
||||
disrupt
|
||||
}
|
||||
ast = fold_mod(ast)
|
||||
return ast
|
||||
}
|
||||
|
||||
// Load a module from .mach bytecode, falling back to source compilation
|
||||
function load_module(name, env) {
|
||||
var mach_path = core_path + '/' + name + ".mach"
|
||||
var data = null
|
||||
var src_path = null
|
||||
var src = null
|
||||
var ast = null
|
||||
var compiled = null
|
||||
var optimized = null
|
||||
if (fd.is_file(mach_path)) {
|
||||
data = fd.slurp(mach_path)
|
||||
return mach_load(data, env)
|
||||
}
|
||||
src_path = core_path + '/' + name + ".cm"
|
||||
src = text(fd.slurp(src_path))
|
||||
ast = analyze(src, src_path)
|
||||
compiled = mcode_mod(ast)
|
||||
optimized = streamline_mod(compiled)
|
||||
return mach_eval_mcode(name, json.encode(optimized), env)
|
||||
}
|
||||
|
||||
// Load optimization pipeline modules (needs analyze to be defined)
|
||||
var qbe_macros = null
|
||||
streamline_mod = load_module("streamline", boot_env)
|
||||
use_cache['streamline'] = streamline_mod
|
||||
if (emit_qbe) {
|
||||
qbe_macros = load_module("qbe", boot_env)
|
||||
qbe_emit_mod = load_module("qbe_emit", boot_env)
|
||||
use_cache['qbe'] = qbe_macros
|
||||
use_cache['qbe_emit'] = qbe_emit_mod
|
||||
}
|
||||
|
||||
// Run AST through mcode pipeline → register VM
|
||||
function run_ast(name, ast, env) {
|
||||
var compiled = mcode_mod(ast)
|
||||
var optimized = streamline_mod(compiled)
|
||||
var qbe_il = null
|
||||
if (emit_qbe) {
|
||||
qbe_il = qbe_emit_mod(optimized, qbe_macros)
|
||||
print(qbe_il)
|
||||
return null
|
||||
}
|
||||
if (dump_mach) {
|
||||
mach_dump_mcode(name, json.encode(optimized), env)
|
||||
return null
|
||||
}
|
||||
return mach_eval_mcode(name, json.encode(optimized), env)
|
||||
}
|
||||
|
||||
// use() with ƿit pipeline for .cm modules
|
||||
function use_fn(path) {
|
||||
var file_path = null
|
||||
var mach_path = null
|
||||
var data = null
|
||||
var script = null
|
||||
var ast = null
|
||||
var result = null
|
||||
if (use_cache[path])
|
||||
return use_cache[path]
|
||||
|
||||
// Try .mach bytecode first (CWD then core_path)
|
||||
mach_path = path + '.mach'
|
||||
if (!fd.is_file(mach_path))
|
||||
mach_path = core_path + '/' + path + '.mach'
|
||||
if (fd.is_file(mach_path)) {
|
||||
data = fd.slurp(mach_path)
|
||||
result = mach_load(data, {use: use_fn})
|
||||
use_cache[path] = result
|
||||
return result
|
||||
}
|
||||
|
||||
// Try .cm source (CWD then core_path)
|
||||
file_path = path + '.cm'
|
||||
if (!fd.is_file(file_path))
|
||||
file_path = core_path + '/' + path + '.cm'
|
||||
|
||||
if (fd.is_file(file_path)) {
|
||||
script = text(fd.slurp(file_path))
|
||||
ast = analyze(script, file_path)
|
||||
result = run_ast(path, ast, {use: use_fn})
|
||||
use_cache[path] = result
|
||||
return result
|
||||
}
|
||||
|
||||
// Fallback to embedded C module
|
||||
result = use_embed(replace(path, '/', '_'))
|
||||
use_cache[path] = result
|
||||
return result
|
||||
}
|
||||
|
||||
// Helper to load engine.cm and run it with given env
|
||||
function load_engine(env) {
|
||||
var engine_path = core_path + '/internal/engine.mach'
|
||||
var data = null
|
||||
var engine_src = null
|
||||
var engine_ast = null
|
||||
if (fd.is_file(engine_path)) {
|
||||
data = fd.slurp(engine_path)
|
||||
return mach_load(data, env)
|
||||
}
|
||||
engine_path = core_path + '/internal/engine.cm'
|
||||
engine_src = text(fd.slurp(engine_path))
|
||||
engine_ast = analyze(engine_src, engine_path)
|
||||
return run_ast('engine', engine_ast, env)
|
||||
}
|
||||
|
||||
// Detect mode and route
|
||||
// CLI mode has 'args'; actor spawn mode has 'init'
|
||||
var program = null
|
||||
var user_args = []
|
||||
var _j = 0
|
||||
var script_file = null
|
||||
var script = null
|
||||
var ast = null
|
||||
|
||||
if (args != null) {
|
||||
// CLI mode — parse args
|
||||
program = args[0]
|
||||
_j = 1
|
||||
while (_j < length(args)) {
|
||||
push(user_args, args[_j])
|
||||
_j = _j + 1
|
||||
}
|
||||
|
||||
// Resolve script file: try .cm then .ce in CWD then core_path
|
||||
script_file = program
|
||||
if (!ends_with(script_file, '.ce') && !ends_with(script_file, '.cm'))
|
||||
script_file = program + '.cm'
|
||||
|
||||
if (!fd.is_file(script_file))
|
||||
script_file = core_path + '/' + program + '.cm'
|
||||
if (!fd.is_file(script_file))
|
||||
script_file = program + '.ce'
|
||||
if (!fd.is_file(script_file))
|
||||
script_file = core_path + '/' + program + '.ce'
|
||||
|
||||
if (ends_with(script_file, '.ce')) {
|
||||
// Actor script — delegate to engine
|
||||
load_engine({
|
||||
os: os, actorsym: actorsym,
|
||||
init: {program: program, arg: user_args},
|
||||
core_path: core_path, shop_path: shop_path, json: json,
|
||||
analyze: analyze, run_ast_fn: run_ast
|
||||
})
|
||||
} else {
|
||||
// Module script — run directly
|
||||
script = text(fd.slurp(script_file))
|
||||
ast = analyze(script, script_file)
|
||||
run_ast(program, ast, {use: use_fn, args: user_args, json: json})
|
||||
}
|
||||
} else {
|
||||
// Actor spawn mode — load engine.cm with full actor env
|
||||
load_engine({
|
||||
os: os, actorsym: actorsym, init: init,
|
||||
core_path: core_path, shop_path: shop_path, json: json, nota: nota, wota: wota,
|
||||
analyze: analyze, run_ast_fn: run_ast
|
||||
})
|
||||
}
|
||||
BIN
internal/bootstrap.mach
Normal file
BIN
internal/bootstrap.mach
Normal file
Binary file not shown.
@@ -1,24 +1,18 @@
|
||||
(function engine() {
|
||||
var _cell = globalThis.cell
|
||||
delete globalThis.cell
|
||||
var ACTORDATA = _cell.hidden.actorsym
|
||||
// Hidden vars (os, actorsym, init, core_path, shop_path, analyze, run_ast_fn, json) come from env
|
||||
// In actor spawn mode, also: nota, wota
|
||||
var ACTORDATA = actorsym
|
||||
var SYSYM = '__SYSTEM__'
|
||||
|
||||
var hidden = _cell.hidden
|
||||
var _cell = {}
|
||||
var need_stop = false
|
||||
|
||||
var os = hidden.os;
|
||||
var cases = {
|
||||
Windows: '.dll',
|
||||
macOS: '.dylib',
|
||||
Linux: '.so'
|
||||
}
|
||||
|
||||
_cell.os = null
|
||||
|
||||
var dylib_ext
|
||||
|
||||
_cell.id ??= "newguy"
|
||||
|
||||
switch(os.platform()) {
|
||||
case 'Windows': dylib_ext = '.dll'; break;
|
||||
case 'macOS': dylib_ext = '.dylib'; break;
|
||||
case 'Linux': dylib_ext = '.so'; break;
|
||||
}
|
||||
var dylib_ext = cases[os.platform()]
|
||||
|
||||
var MOD_EXT = '.cm'
|
||||
var ACTOR_EXT = '.ce'
|
||||
@@ -28,8 +22,7 @@ function use_embed(name) {
|
||||
return load_internal("js_" + name + "_use")
|
||||
}
|
||||
|
||||
globalThis.logical = function(val1)
|
||||
{
|
||||
function logical(val1) {
|
||||
if (val1 == 0 || val1 == false || val1 == "false" || val1 == null)
|
||||
return false;
|
||||
if (val1 == 1 || val1 == true || val1 == "true")
|
||||
@@ -37,70 +30,81 @@ globalThis.logical = function(val1)
|
||||
return null;
|
||||
}
|
||||
|
||||
globalThis.some = function(arr, pred) {
|
||||
function some(arr, pred) {
|
||||
return find(arr, pred) != null
|
||||
}
|
||||
|
||||
globalThis.every = function(arr, pred) {
|
||||
function every(arr, pred) {
|
||||
return find(arr, x => not(pred(x))) == null
|
||||
}
|
||||
|
||||
globalThis.starts_with = function(str, prefix) {
|
||||
function starts_with(str, prefix) {
|
||||
return search(str, prefix) == 0
|
||||
}
|
||||
|
||||
globalThis.ends_with = function(str, suffix) {
|
||||
function ends_with(str, suffix) {
|
||||
return search(str, suffix, -length(suffix)) != null
|
||||
}
|
||||
|
||||
var js = use_embed('js')
|
||||
var fd = use_embed('fd')
|
||||
var js = use_embed('js')
|
||||
|
||||
// Get the shop path from HOME environment
|
||||
var home = os.getenv('HOME') || os.getenv('USERPROFILE')
|
||||
if (!home) {
|
||||
throw Error('Could not determine home directory')
|
||||
}
|
||||
var shop_path = home + '/.cell'
|
||||
var packages_path = shop_path + '/packages'
|
||||
var core_path = packages_path + '/core'
|
||||
|
||||
if (!fd.is_dir(core_path)) {
|
||||
throw Error('Cell shop not found at ' + shop_path + '. Run "cell install" to set up.')
|
||||
}
|
||||
// core_path and shop_path come from env (bootstrap.cm passes them through)
|
||||
// shop_path may be null if --core was used without --shop
|
||||
var packages_path = shop_path ? shop_path + '/packages' : null
|
||||
|
||||
var use_cache = {}
|
||||
use_cache['core/os'] = os
|
||||
|
||||
// Extra env properties added as engine initializes (log, runtime fns, etc.)
|
||||
var core_extras = {}
|
||||
|
||||
// Load a core module from the file system
|
||||
function use_core(path) {
|
||||
var cache_key = 'core/' + path
|
||||
var env = null
|
||||
if (use_cache[cache_key])
|
||||
return use_cache[cache_key];
|
||||
return use_cache[cache_key]
|
||||
|
||||
var sym = use_embed(replace(path, '/', '_'))
|
||||
var result = null
|
||||
var script = null
|
||||
var ast = null
|
||||
|
||||
// Core scripts are in packages/core/
|
||||
var file_path = core_path + '/' + path + MOD_EXT
|
||||
// Build env: merge core_extras, include C embed as 'native' if available
|
||||
env = {use: use_core}
|
||||
arrfor(array(core_extras), function(k) { env[k] = core_extras[k] })
|
||||
if (sym) env.native = sym
|
||||
|
||||
if (fd.is_file(file_path)) {
|
||||
var script_blob = fd.slurp(file_path)
|
||||
var script = text(script_blob)
|
||||
var mod = `(function setup_module(use){${script}})`
|
||||
var fn = js.eval('core:' + path, mod)
|
||||
var result = call(fn,sym, [use_core])
|
||||
use_cache[cache_key] = result;
|
||||
return result;
|
||||
// Check for pre-compiled .mach file first
|
||||
var mach_path = core_path + '/' + path + '.mach'
|
||||
if (fd.is_file(mach_path)) {
|
||||
result = mach_load(fd.slurp(mach_path), env)
|
||||
use_cache[cache_key] = result
|
||||
return result
|
||||
}
|
||||
|
||||
use_cache[cache_key] = sym;
|
||||
return sym;
|
||||
// Fall back to source .cm file — compile at runtime
|
||||
var file_path = core_path + '/' + path + MOD_EXT
|
||||
if (fd.is_file(file_path)) {
|
||||
script = text(fd.slurp(file_path))
|
||||
ast = analyze(script, file_path)
|
||||
result = run_ast_fn('core:' + path, ast, env)
|
||||
use_cache[cache_key] = result
|
||||
return result
|
||||
}
|
||||
|
||||
// Embedded C module only
|
||||
use_cache[cache_key] = sym
|
||||
return sym
|
||||
}
|
||||
|
||||
// Load full modules via use_core (extends C embeds with .cm additions, and caches)
|
||||
fd = use_core('fd')
|
||||
use_core('js')
|
||||
var blob = use_core('blob')
|
||||
|
||||
globalThis.actor = function()
|
||||
{
|
||||
function actor() {
|
||||
|
||||
}
|
||||
|
||||
@@ -108,29 +112,16 @@ var actor_mod = use_core('actor')
|
||||
var wota = use_core('wota')
|
||||
var nota = use_core('nota')
|
||||
|
||||
globalThis.is_actor = function(value) {
|
||||
function is_actor(value) {
|
||||
return is_object(value) && value[ACTORDATA]
|
||||
}
|
||||
|
||||
var ENETSERVICE = 0.1
|
||||
var REPLYTIMEOUT = 60 // seconds before replies are ignored
|
||||
|
||||
function caller_data(depth = 0)
|
||||
function caller_data(depth)
|
||||
{
|
||||
var file = "nofile"
|
||||
var line = 0
|
||||
|
||||
var caller = array(Error().stack, "\n")[1+depth]
|
||||
if (caller) {
|
||||
var md = extract(caller, /\((.*)\:/)
|
||||
var m = md ? md[1] : "SCRIPT"
|
||||
if (m) file = m
|
||||
md = extract(caller, /\:(\d*)\)/)
|
||||
m = md ? md[1] : 0
|
||||
if (m) line = m
|
||||
}
|
||||
|
||||
return {file,line}
|
||||
return {file: "nofile", line: 0}
|
||||
}
|
||||
|
||||
function console_rec(line, file, msg) {
|
||||
@@ -138,49 +129,45 @@ function console_rec(line, file, msg) {
|
||||
// time: [${time.text("mb d yyyy h:nn:ss")}]
|
||||
}
|
||||
|
||||
globalThis.log = function(name, args) {
|
||||
function log(name, args) {
|
||||
var caller = caller_data(1)
|
||||
var msg = args[0]
|
||||
|
||||
switch(name) {
|
||||
case 'console':
|
||||
os.print(console_rec(caller.line, caller.file, msg))
|
||||
break
|
||||
case 'error':
|
||||
msg = msg ?? Error()
|
||||
if (is_proto(msg, Error))
|
||||
msg = msg.name + ": " + msg.message + "\n" + msg.stack
|
||||
os.print(console_rec(caller.line, caller.file, msg))
|
||||
break
|
||||
case 'system':
|
||||
msg = "[SYSTEM] " + msg
|
||||
os.print(console_rec(caller.line, caller.file, msg))
|
||||
break
|
||||
default:
|
||||
log.console(`unknown log type: ${name}`)
|
||||
break
|
||||
|
||||
if (name == 'console') {
|
||||
os.print(console_rec(caller.line, caller.file, msg))
|
||||
} else if (name == 'error') {
|
||||
if (msg == null) msg = "error"
|
||||
os.print(console_rec(caller.line, caller.file, msg))
|
||||
} else if (name == 'system') {
|
||||
msg = "[SYSTEM] " + msg
|
||||
os.print(console_rec(caller.line, caller.file, msg))
|
||||
} else {
|
||||
log.console(`unknown log type: ${name}`)
|
||||
}
|
||||
}
|
||||
|
||||
function disrupt(err)
|
||||
function actor_die(err)
|
||||
{
|
||||
if (is_function(err.toString)) {
|
||||
os.print(err.toString())
|
||||
os.print("\n")
|
||||
os.print(err.stack)
|
||||
var reason = null
|
||||
var unders = null
|
||||
|
||||
if (err && is_function(err.toString)) {
|
||||
os.print(err.toString())
|
||||
os.print("\n")
|
||||
if (err.stack) os.print(err.stack)
|
||||
}
|
||||
|
||||
if (overling) {
|
||||
if (err) {
|
||||
// with an err, this is a forceful disrupt
|
||||
var reason = (is_proto(err, Error)) ? err.stack : err
|
||||
reason = err
|
||||
report_to_overling({type:'disrupt', reason})
|
||||
} else
|
||||
report_to_overling({type:'stop'})
|
||||
}
|
||||
|
||||
if (underlings) {
|
||||
var unders = array(underlings)
|
||||
unders = array(underlings)
|
||||
arrfor(unders, function(id, index) {
|
||||
log.console(`calling on ${id} to disrupt too`)
|
||||
$_.stop(create_actor({id}))
|
||||
@@ -194,20 +181,20 @@ function disrupt(err)
|
||||
log.console(err.stack)
|
||||
}
|
||||
|
||||
actor_mod.disrupt()
|
||||
actor_mod["disrupt"]()
|
||||
}
|
||||
|
||||
|
||||
|
||||
actor_mod.on_exception(disrupt)
|
||||
//actor_mod.on_exception(actor_die)
|
||||
|
||||
_cell.args = _cell.hidden.init
|
||||
_cell.args ??= {}
|
||||
_cell.id ??= "newguy"
|
||||
_cell.args = init != null ? init : {}
|
||||
_cell.id = "newguy"
|
||||
|
||||
function create_actor(desc = {id:guid()}) {
|
||||
function create_actor(desc) {
|
||||
var _desc = desc == null ? {id:guid()} : desc
|
||||
var actor = {}
|
||||
actor[ACTORDATA] = desc
|
||||
actor[ACTORDATA] = _desc
|
||||
return actor
|
||||
}
|
||||
|
||||
@@ -217,25 +204,54 @@ $_.self = create_actor()
|
||||
os.use_cache = use_cache
|
||||
os.global_shop_path = shop_path
|
||||
os.$_ = $_
|
||||
os.analyze = analyze
|
||||
os.run_ast_fn = run_ast_fn
|
||||
os.json = json
|
||||
use_cache['core/json'] = json
|
||||
|
||||
var shop = use_core('internal/shop')
|
||||
|
||||
var json = use_core('json')
|
||||
var time = use_core('time')
|
||||
|
||||
var pronto = use_core('pronto')
|
||||
globalThis.fallback = pronto.fallback
|
||||
globalThis.parallel = pronto.parallel
|
||||
globalThis.race = pronto.race
|
||||
globalThis.sequence = pronto.sequence
|
||||
var fallback = pronto.fallback
|
||||
var parallel = pronto.parallel
|
||||
var race = pronto.race
|
||||
var sequence = pronto.sequence
|
||||
|
||||
// Create runtime environment for modules
|
||||
var runtime_env = {
|
||||
logical: logical,
|
||||
some: some,
|
||||
every: every,
|
||||
starts_with: starts_with,
|
||||
ends_with: ends_with,
|
||||
actor: actor,
|
||||
is_actor: is_actor,
|
||||
log: log,
|
||||
send: send,
|
||||
fallback: fallback,
|
||||
parallel: parallel,
|
||||
race: race,
|
||||
sequence: sequence
|
||||
}
|
||||
|
||||
// Make runtime functions available to modules loaded via use_core
|
||||
arrfor(array(runtime_env), function(k) { core_extras[k] = runtime_env[k] })
|
||||
|
||||
// Pass to os for shop to access
|
||||
os.runtime_env = runtime_env
|
||||
|
||||
$_.time_limit = function(requestor, seconds)
|
||||
{
|
||||
if (!pronto.is_requestor(requestor))
|
||||
throw Error('time_limit: first argument must be a requestor');
|
||||
if (!is_number(seconds) || seconds <= 0)
|
||||
throw Error('time_limit: seconds must be a positive number');
|
||||
|
||||
if (!pronto.is_requestor(requestor)) {
|
||||
log.error('time_limit: first argument must be a requestor')
|
||||
disrupt
|
||||
}
|
||||
if (!is_number(seconds) || seconds <= 0) {
|
||||
log.error('time_limit: seconds must be a positive number')
|
||||
disrupt
|
||||
}
|
||||
|
||||
return function time_limit_requestor(callback, value) {
|
||||
pronto.check_callback(callback, 'time_limit')
|
||||
var finished = false
|
||||
@@ -250,7 +266,14 @@ $_.time_limit = function(requestor, seconds)
|
||||
timer_cancel = null
|
||||
}
|
||||
if (requestor_cancel) {
|
||||
try { pronto.requestor_cancel(reason) } catch (_) {}
|
||||
requestor_cancel(reason)
|
||||
requestor_cancel = null
|
||||
}
|
||||
}
|
||||
|
||||
function safe_cancel_requestor(reason) {
|
||||
if (requestor_cancel) {
|
||||
requestor_cancel(reason)
|
||||
requestor_cancel = null
|
||||
}
|
||||
}
|
||||
@@ -258,15 +281,12 @@ $_.time_limit = function(requestor, seconds)
|
||||
timer_cancel = $_.delay(function() {
|
||||
if (finished) return
|
||||
def reason = make_reason(factory, 'Timeout.', seconds)
|
||||
if (requestor_cancel) {
|
||||
try { requestor_cancel(reason) } catch (_) {}
|
||||
requestor_cancel = null
|
||||
}
|
||||
safe_cancel_requestor(reason)
|
||||
finished = true
|
||||
callback(null, reason)
|
||||
}, seconds)
|
||||
|
||||
try {
|
||||
function do_request() {
|
||||
requestor_cancel = requestor(function(val, reason) {
|
||||
if (finished) return
|
||||
finished = true
|
||||
@@ -276,16 +296,14 @@ $_.time_limit = function(requestor, seconds)
|
||||
}
|
||||
callback(val, reason)
|
||||
}, value)
|
||||
} catch (ex) {
|
||||
cancel(ex)
|
||||
callback(null, ex)
|
||||
} disruption {
|
||||
cancel('requestor failed')
|
||||
callback(null, 'requestor failed')
|
||||
}
|
||||
do_request()
|
||||
|
||||
return function(reason) {
|
||||
if (requestor_cancel) {
|
||||
try { requestor_cancel(reason) } catch (_) {}
|
||||
requestor_cancel = null
|
||||
}
|
||||
safe_cancel_requestor(reason)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -328,9 +346,10 @@ REPLYTIMEOUT = config.reply_timeout
|
||||
}
|
||||
*/
|
||||
|
||||
function guid(bits = 256)
|
||||
function guid(bits)
|
||||
{
|
||||
var guid = blob(bits, os.random)
|
||||
var _bits = bits == null ? 256 : bits
|
||||
var guid = blob(_bits, os.random)
|
||||
stone(guid)
|
||||
return text(guid,'h')
|
||||
}
|
||||
@@ -398,55 +417,61 @@ var portal_fn = null
|
||||
|
||||
// takes a function input value that will eventually be called with the current time in number form.
|
||||
$_.portal = function(fn, port) {
|
||||
if (portal) throw Error(`Already started a portal listening on ${portal.port}`)
|
||||
if (!port) throw Error("Requires a valid port.")
|
||||
if (portal) {
|
||||
log.error(`Already started a portal listening on ${portal.port}`)
|
||||
disrupt
|
||||
}
|
||||
if (!port) {
|
||||
log.error("Requires a valid port.")
|
||||
disrupt
|
||||
}
|
||||
log.system(`starting a portal on port ${port}`)
|
||||
portal = enet.create_host({address: "any", port})
|
||||
portal_fn = fn
|
||||
}
|
||||
|
||||
function handle_host(e) {
|
||||
switch (e.type) {
|
||||
case "connect":
|
||||
log.system(`connected a new peer: ${e.peer.address}:${e.peer.port}`)
|
||||
peers[`${e.peer.address}:${e.peer.port}`] = e.peer
|
||||
var queue = peer_queue.get(e.peer)
|
||||
if (queue) {
|
||||
arrfor(queue, (msg, index) => e.peer.send(nota.encode(msg)))
|
||||
log.system(`sent ${msg} out of queue`)
|
||||
peer_queue.delete(e.peer)
|
||||
}
|
||||
break
|
||||
case "disconnect":
|
||||
var queue = null
|
||||
var data = null
|
||||
|
||||
if (e.type == "connect") {
|
||||
log.system(`connected a new peer: ${e.peer.address}:${e.peer.port}`)
|
||||
peers[`${e.peer.address}:${e.peer.port}`] = e.peer
|
||||
queue = peer_queue.get(e.peer)
|
||||
if (queue) {
|
||||
arrfor(queue, (msg, index) => e.peer.send(nota.encode(msg)))
|
||||
log.system(`sent queue out of queue`)
|
||||
peer_queue.delete(e.peer)
|
||||
arrfor(array(peers), function(id, index) {
|
||||
if (peers[id] == e.peer) delete peers[id]
|
||||
})
|
||||
log.system('portal got disconnect from ' + e.peer.address + ":" + e.peer.port)
|
||||
break
|
||||
case "receive":
|
||||
var data = nota.decode(e.data)
|
||||
if (data.replycc && !data.replycc.address) {
|
||||
data.replycc[ACTORDATA].address = e.peer.address
|
||||
data.replycc[ACTORDATA].port = e.peer.port
|
||||
}
|
||||
function populate_actor_addresses(obj) {
|
||||
if (!is_object(obj)) return
|
||||
if (obj[ACTORDATA] && !obj[ACTORDATA].address) {
|
||||
obj[ACTORDATA].address = e.peer.address
|
||||
obj[ACTORDATA].port = e.peer.port
|
||||
}
|
||||
arrfor(array(obj), function(key, index) {
|
||||
if (key in obj)
|
||||
populate_actor_addresses(obj[key])
|
||||
})
|
||||
}
|
||||
if (data.data) populate_actor_addresses(data.data)
|
||||
turn(data)
|
||||
break
|
||||
}
|
||||
} else if (e.type == "disconnect") {
|
||||
peer_queue.delete(e.peer)
|
||||
arrfor(array(peers), function(id, index) {
|
||||
if (peers[id] == e.peer) delete peers[id]
|
||||
})
|
||||
log.system('portal got disconnect from ' + e.peer.address + ":" + e.peer.port)
|
||||
} else if (e.type == "receive") {
|
||||
data = nota.decode(e.data)
|
||||
if (data.replycc && !data.replycc.address) {
|
||||
data.replycc[ACTORDATA].address = e.peer.address
|
||||
data.replycc[ACTORDATA].port = e.peer.port
|
||||
}
|
||||
if (data.data) populate_actor_addresses(data.data, e)
|
||||
turn(data)
|
||||
}
|
||||
}
|
||||
|
||||
function populate_actor_addresses(obj, e) {
|
||||
if (!is_object(obj)) return
|
||||
if (obj[ACTORDATA] && !obj[ACTORDATA].address) {
|
||||
obj[ACTORDATA].address = e.peer.address
|
||||
obj[ACTORDATA].port = e.peer.port
|
||||
}
|
||||
arrfor(array(obj), function(key, index) {
|
||||
if (key in obj)
|
||||
populate_actor_addresses(obj[key], e)
|
||||
})
|
||||
}
|
||||
|
||||
// takes a callback function, an actor object, and a configuration record for getting information about the status of a connection to the actor. The configuration record is used to request the sort of information that needs to be communicated. This can include latency, bandwidth, activity, congestion, cost, partitions. The callback is given a record containing the requested information.
|
||||
$_.contact = function(callback, record) {
|
||||
send(create_actor(record), record, callback)
|
||||
@@ -477,10 +502,14 @@ $_.stop = function stop(actor) {
|
||||
need_stop = true
|
||||
return
|
||||
}
|
||||
if (!is_actor(actor))
|
||||
throw Error('Can only call stop on an actor.')
|
||||
if (is_null(underlings[actor[ACTORDATA].id]))
|
||||
throw Error('Can only call stop on an underling or self.')
|
||||
if (!is_actor(actor)) {
|
||||
log.error('Can only call stop on an actor.')
|
||||
disrupt
|
||||
}
|
||||
if (is_null(underlings[actor[ACTORDATA].id])) {
|
||||
log.error('Can only call stop on an underling or self.')
|
||||
disrupt
|
||||
}
|
||||
|
||||
sys_msg(actor, {kind:"stop"})
|
||||
}
|
||||
@@ -491,12 +520,13 @@ $_.unneeded = function unneeded(fn, seconds) {
|
||||
}
|
||||
|
||||
// schedules the invocation of a function after a specified amount of time.
|
||||
$_.delay = function delay(fn, seconds = 0) {
|
||||
$_.delay = function delay(fn, seconds) {
|
||||
var _seconds = seconds == null ? 0 : seconds
|
||||
function delay_turn() {
|
||||
fn()
|
||||
send_messages()
|
||||
}
|
||||
var id = actor_mod.delay(delay_turn, seconds)
|
||||
var id = actor_mod.delay(delay_turn, _seconds)
|
||||
return function() { actor_mod.removetimer(id) }
|
||||
}
|
||||
|
||||
@@ -517,42 +547,46 @@ function actor_prep(actor, send) {
|
||||
|
||||
// Send a message immediately without queuing
|
||||
function actor_send_immediate(actor, send) {
|
||||
try {
|
||||
actor_send(actor, send);
|
||||
} catch (err) {
|
||||
log.error("Failed to send immediate message:", err);
|
||||
}
|
||||
actor_send(actor, send)
|
||||
}
|
||||
|
||||
function actor_send(actor, message) {
|
||||
var wota_blob = null
|
||||
var peer = null
|
||||
|
||||
if (actor[HEADER] && !actor[HEADER].replycc) // attempting to respond to a message but sender is not expecting; silently drop
|
||||
return
|
||||
|
||||
if (!is_actor(actor) && !is_actor(actor.replycc)) throw Error(`Must send to an actor object. Attempted send to ${actor}`)
|
||||
|
||||
if (!is_object(message)) throw Error('Must send an object record.')
|
||||
if (!is_actor(actor) && !is_actor(actor.replycc)) {
|
||||
log.error(`Must send to an actor object. Attempted send to ${actor}`)
|
||||
disrupt
|
||||
}
|
||||
|
||||
if (!is_object(message)) {
|
||||
log.error('Must send an object record.')
|
||||
disrupt
|
||||
}
|
||||
|
||||
// message to self
|
||||
if (actor[ACTORDATA].id == _cell.id) {
|
||||
if (receive_fn) receive_fn(message.data)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
// message to actor in same flock
|
||||
if (actor[ACTORDATA].id && actor_mod.mailbox_exist(actor[ACTORDATA].id)) {
|
||||
var wota_blob = wota.encode(message)
|
||||
// log.console(`sending wota blob of ${length(wota_blob)/8} bytes`)
|
||||
wota_blob = wota.encode(message)
|
||||
actor_mod.mailbox_push(actor[ACTORDATA].id, wota_blob)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
if (actor[ACTORDATA].address) {
|
||||
if (actor[ACTORDATA].id)
|
||||
message.target = actor[ACTORDATA].id
|
||||
else
|
||||
message.type = "contact"
|
||||
|
||||
var peer = peers[actor[ACTORDATA].address + ":" + actor[ACTORDATA].port]
|
||||
|
||||
peer = peers[actor[ACTORDATA].address + ":" + actor[ACTORDATA].port]
|
||||
if (!peer) {
|
||||
if (!portal) {
|
||||
log.system(`creating a contactor ...`)
|
||||
@@ -573,12 +607,10 @@ function actor_send(actor, message) {
|
||||
// Holds all messages queued during the current turn.
|
||||
var message_queue = []
|
||||
|
||||
var need_stop = false
|
||||
|
||||
function send_messages() {
|
||||
function send_messages() {
|
||||
// if we've been flagged to stop, bail out before doing anything
|
||||
if (need_stop) {
|
||||
disrupt()
|
||||
actor_die()
|
||||
message_queue = []
|
||||
return
|
||||
}
|
||||
@@ -597,25 +629,37 @@ var need_stop = false
|
||||
|
||||
var replies = {}
|
||||
|
||||
globalThis.send = function send(actor, message, reply) {
|
||||
if (!is_object(actor))
|
||||
throw Error(`Must send to an actor object. Provided: ${actor}`);
|
||||
|
||||
if (!is_object(message))
|
||||
throw Error('Message must be an object')
|
||||
var send = {type:"user", data: message}
|
||||
function send(actor, message, reply) {
|
||||
var send_msg = null
|
||||
var target = null
|
||||
var header = null
|
||||
var id = null
|
||||
|
||||
if (!is_object(actor)) {
|
||||
log.error(`Must send to an actor object. Provided: ${actor}`)
|
||||
disrupt
|
||||
}
|
||||
|
||||
if (!is_object(message)) {
|
||||
log.error('Message must be an object')
|
||||
disrupt
|
||||
}
|
||||
send_msg = {type:"user", data: message}
|
||||
target = actor
|
||||
|
||||
if (actor[HEADER] && actor[HEADER].replycc) {
|
||||
var header = actor[HEADER]
|
||||
if (!header.replycc || !is_actor(header.replycc))
|
||||
throw Error(`Supplied actor had a return, but it's not a valid actor! ${actor[HEADER]}`)
|
||||
header = actor[HEADER]
|
||||
if (!header.replycc || !is_actor(header.replycc)) {
|
||||
log.error(`Supplied actor had a return, but it's not a valid actor! ${actor[HEADER]}`)
|
||||
disrupt
|
||||
}
|
||||
|
||||
actor = header.replycc
|
||||
send.return = header.reply
|
||||
target = header.replycc
|
||||
send_msg.return = header.reply
|
||||
}
|
||||
|
||||
if (reply) {
|
||||
var id = guid()
|
||||
id = guid()
|
||||
replies[id] = reply
|
||||
$_.delay(_ => {
|
||||
if (replies[id]) {
|
||||
@@ -623,12 +667,12 @@ globalThis.send = function send(actor, message, reply) {
|
||||
delete replies[id]
|
||||
}
|
||||
}, REPLYTIMEOUT)
|
||||
send.reply = id
|
||||
send.replycc = $_.self
|
||||
send_msg.reply = id
|
||||
send_msg.replycc = $_.self
|
||||
}
|
||||
|
||||
// Instead of sending immediately, queue it
|
||||
actor_prep(actor,send);
|
||||
actor_prep(target, send_msg);
|
||||
}
|
||||
|
||||
stone(send)
|
||||
@@ -659,7 +703,7 @@ overling = _cell.args.overling
|
||||
$_.overling = overling
|
||||
|
||||
root = _cell.args.root
|
||||
root ??= $_.self
|
||||
if (root == null) root = $_.self
|
||||
|
||||
if (overling) {
|
||||
$_.couple(overling) // auto couple to overling
|
||||
@@ -695,69 +739,71 @@ function handle_actor_disconnect(id) {
|
||||
delete greeters[id]
|
||||
}
|
||||
log.system(`actor ${id} disconnected`)
|
||||
if (!is_null(couplings[id])) disrupt("coupled actor died") // couplings now disrupts instead of stop
|
||||
if (!is_null(couplings[id])) actor_die("coupled actor died") // couplings now disrupts instead of stop
|
||||
}
|
||||
|
||||
function handle_sysym(msg)
|
||||
{
|
||||
var from
|
||||
switch(msg.kind) {
|
||||
case 'stop':
|
||||
disrupt("got stop message")
|
||||
break
|
||||
case 'underling':
|
||||
from = msg.from
|
||||
var greeter = greeters[from[ACTORDATA].id]
|
||||
if (greeter) greeter(msg.message)
|
||||
if (msg.message.type == 'disrupt')
|
||||
delete underlings[from[ACTORDATA].id]
|
||||
break
|
||||
case 'contact':
|
||||
if (portal_fn) {
|
||||
var letter2 = msg.data
|
||||
letter2[HEADER] = msg
|
||||
delete msg.data
|
||||
portal_fn(letter2)
|
||||
} else throw Error('Got a contact message, but no portal is established.')
|
||||
break
|
||||
case 'couple': // from must be notified when we die
|
||||
from = msg.from
|
||||
underlings[from[ACTORDATA].id] = true
|
||||
log.system(`actor ${from} is coupled to me`)
|
||||
break
|
||||
var from = null
|
||||
var greeter = null
|
||||
var letter2 = null
|
||||
|
||||
if (msg.kind == 'stop') {
|
||||
actor_die("got stop message")
|
||||
} else if (msg.kind == 'underling') {
|
||||
from = msg.from
|
||||
greeter = greeters[from[ACTORDATA].id]
|
||||
if (greeter) greeter(msg.message)
|
||||
if (msg.message.type == 'disrupt')
|
||||
delete underlings[from[ACTORDATA].id]
|
||||
} else if (msg.kind == 'contact') {
|
||||
if (portal_fn) {
|
||||
letter2 = msg.data
|
||||
letter2[HEADER] = msg
|
||||
delete msg.data
|
||||
portal_fn(letter2)
|
||||
} else {
|
||||
log.error('Got a contact message, but no portal is established.')
|
||||
disrupt
|
||||
}
|
||||
} else if (msg.kind == 'couple') {
|
||||
// from must be notified when we die
|
||||
from = msg.from
|
||||
underlings[from[ACTORDATA].id] = true
|
||||
log.system(`actor ${from} is coupled to me`)
|
||||
}
|
||||
}
|
||||
|
||||
function handle_message(msg) {
|
||||
var letter = null
|
||||
var fn = null
|
||||
|
||||
if (msg[SYSYM]) {
|
||||
handle_sysym(msg[SYSYM], msg.from)
|
||||
return
|
||||
}
|
||||
|
||||
switch (msg.type) {
|
||||
case "user":
|
||||
var letter = msg.data // what the sender really sent
|
||||
_ObjectDefineProperty(letter, HEADER, {
|
||||
value: msg, enumerable: false
|
||||
})
|
||||
_ObjectDefineProperty(letter, ACTORDATA, { // this is so is_actor == true
|
||||
value: { reply: msg.reply }, enumerable: false
|
||||
})
|
||||
|
||||
if (msg.return) {
|
||||
var fn = replies[msg.return]
|
||||
if (fn) fn(letter)
|
||||
delete replies[msg.return]
|
||||
return
|
||||
}
|
||||
|
||||
if (receive_fn) receive_fn(letter)
|
||||
if (msg.type == "user") {
|
||||
letter = msg.data // what the sender really sent
|
||||
_ObjectDefineProperty(letter, HEADER, {
|
||||
value: msg, enumerable: false
|
||||
})
|
||||
_ObjectDefineProperty(letter, ACTORDATA, { // this is so is_actor == true
|
||||
value: { reply: msg.reply }, enumerable: false
|
||||
})
|
||||
|
||||
if (msg.return) {
|
||||
fn = replies[msg.return]
|
||||
if (fn) fn(letter)
|
||||
delete replies[msg.return]
|
||||
return
|
||||
case "stopped":
|
||||
handle_actor_disconnect(msg.id)
|
||||
break
|
||||
}
|
||||
|
||||
if (receive_fn) receive_fn(letter)
|
||||
} else if (msg.type == "stopped") {
|
||||
handle_actor_disconnect(msg.id)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function enet_check()
|
||||
{
|
||||
@@ -772,45 +818,62 @@ function enet_check()
|
||||
actor_mod.setname(_cell.args.program)
|
||||
|
||||
var prog = _cell.args.program
|
||||
if (ends_with(prog, '.ce')) prog = text(prog, 0, -3)
|
||||
|
||||
var package = use_core('package')
|
||||
|
||||
var locator = shop.resolve_locator(_cell.args.program + ".ce", null)
|
||||
|
||||
if (!locator) {
|
||||
var pkg = package.find_package_dir(_cell.args.program + ".ce")
|
||||
locator = shop.resolve_locator(_cell.args.program + ".ce", pkg)
|
||||
// Find the .ce file
|
||||
var prog_path = prog + ".ce"
|
||||
var pkg_dir = null
|
||||
var core_dir = null
|
||||
if (!fd.is_file(prog_path)) {
|
||||
pkg_dir = package.find_package_dir(prog_path)
|
||||
if (pkg_dir)
|
||||
prog_path = pkg_dir + '/' + prog + '.ce'
|
||||
}
|
||||
if (!fd.is_file(prog_path)) {
|
||||
// Check core packages
|
||||
core_dir = core_path
|
||||
prog_path = core_dir + '/' + prog + '.ce'
|
||||
}
|
||||
if (!fd.is_file(prog_path)) {
|
||||
os.print(`Main program ${prog} could not be found\n`)
|
||||
os.exit(1)
|
||||
}
|
||||
|
||||
if (!locator)
|
||||
throw Error(`Main program ${_cell.args.program} could not be found`)
|
||||
|
||||
stone(globalThis)
|
||||
|
||||
$_.clock(_ => {
|
||||
// Get capabilities for the main program
|
||||
var file_info = shop.file_info ? shop.file_info(locator.path) : null
|
||||
var file_info = shop.file_info ? shop.file_info(prog_path) : null
|
||||
var inject = shop.script_inject_for ? shop.script_inject_for(file_info) : []
|
||||
|
||||
// Build env object for injection
|
||||
// Build env with runtime functions + capability injections
|
||||
var env = {}
|
||||
for (var i = 0; i < length(inject); i++) {
|
||||
var key = inject[i]
|
||||
arrfor(array(runtime_env), function(k) { env[k] = runtime_env[k] })
|
||||
var _ki = 0
|
||||
var inj = null
|
||||
var key = null
|
||||
while (_ki < length(inject)) {
|
||||
inj = inject[_ki]
|
||||
key = inj
|
||||
if (key && key[0] == '$') key = text(key, 1)
|
||||
if (key == 'fd') env[key] = fd
|
||||
else env[key] = $_[key]
|
||||
if (key == 'fd') env['$fd'] = fd
|
||||
else env['$' + key] = $_[key]
|
||||
_ki = _ki + 1
|
||||
}
|
||||
|
||||
// Create use function bound to the program's package
|
||||
var pkg = file_info ? file_info.package : null
|
||||
var use_fn = function(path) { return shop.use(path, pkg) }
|
||||
env.use = function(path) {
|
||||
var ck = 'core/' + path
|
||||
if (use_cache[ck]) return use_cache[ck]
|
||||
return shop.use(path, pkg)
|
||||
}
|
||||
env.args = _cell.args.arg
|
||||
env.log = log
|
||||
|
||||
// Call with signature: setup_module(args, use, env)
|
||||
// The script wrapper binds $delay, $start, etc. from env
|
||||
var val = call(locator.symbol, null, [_cell.args.arg, use_fn, env])
|
||||
|
||||
if (val)
|
||||
throw Error('Program must not return anything');
|
||||
var script = text(fd.slurp(prog_path))
|
||||
var ast = analyze(script, prog_path)
|
||||
var val = run_ast_fn(prog, ast, env)
|
||||
if (val) {
|
||||
log.error('Program must not return anything')
|
||||
disrupt
|
||||
}
|
||||
})
|
||||
|
||||
})()
|
||||
BIN
internal/engine.mach
Normal file
BIN
internal/engine.mach
Normal file
Binary file not shown.
394
internal/nota.c
394
internal/nota.c
@@ -1,394 +0,0 @@
|
||||
#include "cell.h"
|
||||
#include "cell_internal.h"
|
||||
|
||||
#define NOTA_IMPLEMENTATION
|
||||
#include "nota.h"
|
||||
|
||||
typedef struct NotaEncodeContext {
|
||||
JSContext *ctx;
|
||||
JSValue visitedStack;
|
||||
NotaBuffer nb;
|
||||
int cycle;
|
||||
JSValue replacer;
|
||||
} NotaEncodeContext;
|
||||
|
||||
static void nota_stack_push(NotaEncodeContext *enc, JSValueConst val)
|
||||
{
|
||||
JSContext *ctx = enc->ctx;
|
||||
int len = JS_ArrayLength(ctx, enc->visitedStack);
|
||||
JS_SetPropertyInt64(ctx, enc->visitedStack, len, JS_DupValue(ctx, val));
|
||||
}
|
||||
|
||||
static void nota_stack_pop(NotaEncodeContext *enc)
|
||||
{
|
||||
JSContext *ctx = enc->ctx;
|
||||
int len = JS_ArrayLength(ctx, enc->visitedStack);
|
||||
JS_SetPropertyStr(ctx, enc->visitedStack, "length", JS_NewUint32(ctx, len - 1));
|
||||
}
|
||||
|
||||
static int nota_stack_has(NotaEncodeContext *enc, JSValueConst val)
|
||||
{
|
||||
JSContext *ctx = enc->ctx;
|
||||
int len = JS_ArrayLength(ctx, enc->visitedStack);
|
||||
for (int i = 0; i < len; i++) {
|
||||
JSValue elem = JS_GetPropertyUint32(ctx, enc->visitedStack, i);
|
||||
if (JS_IsObject(elem) && JS_IsObject(val)) {
|
||||
if (JS_StrictEq(ctx, elem, val)) {
|
||||
JS_FreeValue(ctx, elem);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
JS_FreeValue(ctx, elem);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static JSValue apply_replacer(NotaEncodeContext *enc, JSValueConst holder, JSValueConst key, JSValueConst val) {
|
||||
if (JS_IsNull(enc->replacer)) return JS_DupValue(enc->ctx, val);
|
||||
|
||||
JSValue args[2] = { JS_DupValue(enc->ctx, key), JS_DupValue(enc->ctx, val) };
|
||||
JSValue result = JS_Call(enc->ctx, enc->replacer, holder, 2, args);
|
||||
JS_FreeValue(enc->ctx, args[0]);
|
||||
JS_FreeValue(enc->ctx, args[1]);
|
||||
|
||||
if (JS_IsException(result)) return JS_DupValue(enc->ctx, val);
|
||||
return result;
|
||||
}
|
||||
|
||||
char *js_do_nota_decode(JSContext *js, JSValue *tmp, char *nota, JSValue holder, JSValue key, JSValue reviver) {
|
||||
int type = nota_type(nota);
|
||||
JSValue ret2;
|
||||
long long n;
|
||||
double d;
|
||||
int b;
|
||||
char *str;
|
||||
uint8_t *blob;
|
||||
|
||||
switch(type) {
|
||||
case NOTA_BLOB:
|
||||
nota = nota_read_blob(&n, (char**)&blob, nota);
|
||||
*tmp = js_new_blob_stoned_copy(js, blob, n);
|
||||
free(blob);
|
||||
break;
|
||||
case NOTA_TEXT:
|
||||
nota = nota_read_text(&str, nota);
|
||||
*tmp = JS_NewString(js, str);
|
||||
free(str);
|
||||
break;
|
||||
case NOTA_ARR:
|
||||
nota = nota_read_array(&n, nota);
|
||||
*tmp = JS_NewArray(js);
|
||||
for (int i = 0; i < n; i++) {
|
||||
nota = js_do_nota_decode(js, &ret2, nota, *tmp, JS_NewInt32(js, i), reviver);
|
||||
JS_SetPropertyInt64(js, *tmp, i, ret2);
|
||||
}
|
||||
break;
|
||||
case NOTA_REC:
|
||||
nota = nota_read_record(&n, nota);
|
||||
*tmp = JS_NewObject(js);
|
||||
for (int i = 0; i < n; i++) {
|
||||
nota = nota_read_text(&str, nota);
|
||||
JSValue prop_key = JS_NewString(js, str);
|
||||
nota = js_do_nota_decode(js, &ret2, nota, *tmp, prop_key, reviver);
|
||||
JS_SetPropertyStr(js, *tmp, str, ret2);
|
||||
JS_FreeValue(js, prop_key);
|
||||
free(str);
|
||||
}
|
||||
break;
|
||||
case NOTA_INT:
|
||||
nota = nota_read_int(&n, nota);
|
||||
*tmp = JS_NewInt64(js, n);
|
||||
break;
|
||||
case NOTA_SYM:
|
||||
nota = nota_read_sym(&b, nota);
|
||||
if (b == NOTA_PRIVATE) {
|
||||
JSValue inner;
|
||||
nota = js_do_nota_decode(js, &inner, nota, holder, JS_NULL, reviver);
|
||||
JSValue obj = JS_NewObject(js);
|
||||
cell_rt *crt = JS_GetContextOpaque(js);
|
||||
// JS_SetProperty(js, obj, crt->actor_sym, inner);
|
||||
*tmp = obj;
|
||||
} else {
|
||||
switch(b) {
|
||||
case NOTA_NULL: *tmp = JS_NULL; break;
|
||||
case NOTA_FALSE: *tmp = JS_NewBool(js, 0); break;
|
||||
case NOTA_TRUE: *tmp = JS_NewBool(js, 1); break;
|
||||
default: *tmp = JS_NULL; break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
case NOTA_FLOAT:
|
||||
nota = nota_read_float(&d, nota);
|
||||
*tmp = JS_NewFloat64(js, d);
|
||||
break;
|
||||
}
|
||||
|
||||
if (!JS_IsNull(reviver)) {
|
||||
JSValue args[2] = { JS_DupValue(js, key), JS_DupValue(js, *tmp) };
|
||||
JSValue revived = JS_Call(js, reviver, holder, 2, args);
|
||||
JS_FreeValue(js, args[0]);
|
||||
JS_FreeValue(js, args[1]);
|
||||
if (!JS_IsException(revived)) {
|
||||
JS_FreeValue(js, *tmp);
|
||||
*tmp = revived;
|
||||
} else {
|
||||
JS_FreeValue(js, revived);
|
||||
}
|
||||
}
|
||||
|
||||
return nota;
|
||||
}
|
||||
|
||||
static void nota_encode_value(NotaEncodeContext *enc, JSValueConst val, JSValueConst holder, JSValueConst key) {
|
||||
JSContext *ctx = enc->ctx;
|
||||
JSValue replaced = apply_replacer(enc, holder, key, val);
|
||||
int tag = JS_VALUE_GET_TAG(replaced);
|
||||
|
||||
switch (tag) {
|
||||
case JS_TAG_INT:
|
||||
case JS_TAG_FLOAT64: {
|
||||
double d;
|
||||
JS_ToFloat64(ctx, &d, replaced);
|
||||
nota_write_number(&enc->nb, d);
|
||||
break;
|
||||
}
|
||||
case JS_TAG_STRING: {
|
||||
const char *str = JS_ToCString(ctx, replaced);
|
||||
nota_write_text(&enc->nb, str);
|
||||
JS_FreeCString(ctx, str);
|
||||
break;
|
||||
}
|
||||
case JS_TAG_BOOL:
|
||||
if (JS_VALUE_GET_BOOL(replaced)) nota_write_sym(&enc->nb, NOTA_TRUE);
|
||||
else nota_write_sym(&enc->nb, NOTA_FALSE);
|
||||
break;
|
||||
case JS_TAG_NULL:
|
||||
nota_write_sym(&enc->nb, NOTA_NULL);
|
||||
break;
|
||||
case JS_TAG_PTR: {
|
||||
if (js_is_blob(ctx, replaced)) {
|
||||
size_t buf_len;
|
||||
void *buf_data = js_get_blob_data(ctx, &buf_len, replaced);
|
||||
if (buf_data == -1) {
|
||||
JS_FreeValue(ctx, replaced);
|
||||
return; // JS_EXCEPTION will be handled by caller
|
||||
}
|
||||
nota_write_blob(&enc->nb, (unsigned long long)buf_len * 8, (const char*)buf_data);
|
||||
break;
|
||||
}
|
||||
|
||||
if (JS_IsArray(replaced)) {
|
||||
if (nota_stack_has(enc, replaced)) {
|
||||
enc->cycle = 1;
|
||||
break;
|
||||
}
|
||||
nota_stack_push(enc, replaced);
|
||||
int arr_len = JS_ArrayLength(ctx, replaced);
|
||||
nota_write_array(&enc->nb, arr_len);
|
||||
for (int i = 0; i < arr_len; i++) {
|
||||
JSValue elem_val = JS_GetPropertyUint32(ctx, replaced, i);
|
||||
JSValue elem_key = JS_NewInt32(ctx, i);
|
||||
nota_encode_value(enc, elem_val, replaced, elem_key);
|
||||
JS_FreeValue(ctx, elem_val);
|
||||
JS_FreeValue(ctx, elem_key);
|
||||
}
|
||||
nota_stack_pop(enc);
|
||||
break;
|
||||
}
|
||||
|
||||
cell_rt *crt = JS_GetContextOpaque(ctx);
|
||||
// JSValue adata = JS_GetProperty(ctx, replaced, crt->actor_sym);
|
||||
JSValue adata = JS_NULL;
|
||||
if (!JS_IsNull(adata)) {
|
||||
nota_write_sym(&enc->nb, NOTA_PRIVATE);
|
||||
nota_encode_value(enc, adata, replaced, JS_NULL);
|
||||
JS_FreeValue(ctx, adata);
|
||||
break;
|
||||
}
|
||||
JS_FreeValue(ctx, adata);
|
||||
if (nota_stack_has(enc, replaced)) {
|
||||
enc->cycle = 1;
|
||||
break;
|
||||
}
|
||||
nota_stack_push(enc, replaced);
|
||||
|
||||
JSValue to_json = JS_GetPropertyStr(ctx, replaced, "toJSON");
|
||||
if (JS_IsFunction(to_json)) {
|
||||
JSValue result = JS_Call(ctx, to_json, replaced, 0, NULL);
|
||||
JS_FreeValue(ctx, to_json);
|
||||
if (!JS_IsException(result)) {
|
||||
nota_encode_value(enc, result, holder, key);
|
||||
JS_FreeValue(ctx, result);
|
||||
} else {
|
||||
nota_write_sym(&enc->nb, NOTA_NULL);
|
||||
}
|
||||
nota_stack_pop(enc);
|
||||
break;
|
||||
}
|
||||
JS_FreeValue(ctx, to_json);
|
||||
|
||||
JSValue keys = JS_GetOwnPropertyNames(ctx, replaced);
|
||||
if (JS_IsException(keys)) {
|
||||
nota_write_sym(&enc->nb, NOTA_NULL);
|
||||
nota_stack_pop(enc);
|
||||
break;
|
||||
}
|
||||
int64_t plen64;
|
||||
if (JS_GetLength(ctx, keys, &plen64) < 0) {
|
||||
JS_FreeValue(ctx, keys);
|
||||
nota_write_sym(&enc->nb, NOTA_NULL);
|
||||
nota_stack_pop(enc);
|
||||
break;
|
||||
}
|
||||
uint32_t plen = (uint32_t)plen64;
|
||||
|
||||
uint32_t non_function_count = 0;
|
||||
for (uint32_t i = 0; i < plen; i++) {
|
||||
JSValue key = JS_GetPropertyUint32(ctx, keys, i);
|
||||
JSValue prop_val = JS_GetProperty(ctx, replaced, key);
|
||||
if (!JS_IsFunction(prop_val)) non_function_count++;
|
||||
JS_FreeValue(ctx, prop_val);
|
||||
JS_FreeValue(ctx, key);
|
||||
}
|
||||
|
||||
nota_write_record(&enc->nb, non_function_count);
|
||||
for (uint32_t i = 0; i < plen; i++) {
|
||||
JSValue key = JS_GetPropertyUint32(ctx, keys, i);
|
||||
JSValue prop_val = JS_GetProperty(ctx, replaced, key);
|
||||
if (!JS_IsFunction(prop_val)) {
|
||||
const char *prop_name = JS_ToCString(ctx, key);
|
||||
nota_write_text(&enc->nb, prop_name ? prop_name : "");
|
||||
nota_encode_value(enc, prop_val, replaced, key);
|
||||
JS_FreeCString(ctx, prop_name);
|
||||
}
|
||||
JS_FreeValue(ctx, prop_val);
|
||||
JS_FreeValue(ctx, key);
|
||||
}
|
||||
JS_FreeValue(ctx, keys);
|
||||
nota_stack_pop(enc);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
nota_write_sym(&enc->nb, NOTA_NULL);
|
||||
break;
|
||||
}
|
||||
JS_FreeValue(ctx, replaced);
|
||||
}
|
||||
|
||||
void *value2nota(JSContext *ctx, JSValue v) {
|
||||
NotaEncodeContext enc_s, *enc = &enc_s;
|
||||
enc->ctx = ctx;
|
||||
enc->visitedStack = JS_NewArray(ctx);
|
||||
enc->cycle = 0;
|
||||
enc->replacer = JS_NULL;
|
||||
|
||||
nota_buffer_init(&enc->nb, 128);
|
||||
nota_encode_value(enc, v, JS_NULL, JS_NewString(ctx, ""));
|
||||
|
||||
if (enc->cycle) {
|
||||
JS_FreeValue(ctx, enc->visitedStack);
|
||||
nota_buffer_free(&enc->nb);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
JS_FreeValue(ctx, enc->visitedStack);
|
||||
void *data_ptr = enc->nb.data;
|
||||
enc->nb.data = NULL;
|
||||
nota_buffer_free(&enc->nb);
|
||||
return data_ptr;
|
||||
}
|
||||
|
||||
JSValue nota2value(JSContext *js, void *nota) {
|
||||
if (!nota) return JS_NULL;
|
||||
JSValue ret;
|
||||
JSValue holder = JS_NewObject(js);
|
||||
js_do_nota_decode(js, &ret, nota, holder, JS_NewString(js, ""), JS_NULL);
|
||||
JS_FreeValue(js, holder);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static JSValue js_nota_tostring(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) {
|
||||
size_t len;
|
||||
void *nota = js_get_blob_data(ctx, &len, this_val);
|
||||
if (nota == (void*)-1) return JS_EXCEPTION;
|
||||
if (!nota) return JS_NULL;
|
||||
|
||||
JSValue decoded;
|
||||
JSValue holder = JS_NewObject(ctx);
|
||||
js_do_nota_decode(ctx, &decoded, (char*)nota, holder, JS_NewString(ctx, ""), JS_NULL);
|
||||
JS_FreeValue(ctx, holder);
|
||||
|
||||
JSValue global = JS_GetGlobalObject(ctx);
|
||||
JSValue json = JS_GetPropertyStr(ctx, global, "JSON");
|
||||
JSValue stringify = JS_GetPropertyStr(ctx, json, "stringify");
|
||||
|
||||
JSValue args[3];
|
||||
args[0] = decoded;
|
||||
args[1] = JS_NULL;
|
||||
args[2] = JS_NewInt32(ctx, 1);
|
||||
|
||||
JSValue result = JS_Call(ctx, stringify, json, 3, args);
|
||||
|
||||
JS_FreeValue(ctx, stringify);
|
||||
JS_FreeValue(ctx, json);
|
||||
JS_FreeValue(ctx, global);
|
||||
JS_FreeValue(ctx, decoded);
|
||||
JS_FreeValue(ctx, args[2]);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static JSValue js_nota_encode(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) {
|
||||
if (argc < 1) return JS_ThrowTypeError(ctx, "nota.encode requires at least 1 argument");
|
||||
|
||||
NotaEncodeContext enc_s, *enc = &enc_s;
|
||||
enc->ctx = ctx;
|
||||
enc->visitedStack = JS_NewArray(ctx);
|
||||
enc->cycle = 0;
|
||||
enc->replacer = (argc > 1 && JS_IsFunction(argv[1])) ? argv[1] : JS_NULL;
|
||||
|
||||
nota_buffer_init(&enc->nb, 128);
|
||||
nota_encode_value(enc, argv[0], JS_NULL, JS_NewString(ctx, ""));
|
||||
|
||||
if (enc->cycle) {
|
||||
JS_FreeValue(ctx, enc->visitedStack);
|
||||
nota_buffer_free(&enc->nb);
|
||||
return JS_ThrowReferenceError(ctx, "Tried to encode something to nota with a cycle.");
|
||||
}
|
||||
|
||||
JS_FreeValue(ctx, enc->visitedStack);
|
||||
size_t total_len = enc->nb.size;
|
||||
void *data_ptr = enc->nb.data;
|
||||
JSValue ret = js_new_blob_stoned_copy(ctx, (uint8_t*)data_ptr, total_len);
|
||||
|
||||
nota_buffer_free(&enc->nb);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static JSValue js_nota_decode(JSContext *js, JSValueConst self, int argc, JSValueConst *argv) {
|
||||
if (argc < 1) return JS_NULL;
|
||||
|
||||
size_t len;
|
||||
unsigned char *nota = js_get_blob_data(js, &len, argv[0]);
|
||||
if (nota == -1) return JS_EXCEPTION;
|
||||
if (!nota) return JS_NULL;
|
||||
|
||||
JSValue reviver = (argc > 1 && JS_IsFunction(argv[1])) ? argv[1] : JS_NULL;
|
||||
JSValue ret;
|
||||
JSValue holder = JS_NewObject(js);
|
||||
js_do_nota_decode(js, &ret, (char*)nota, holder, JS_NewString(js, ""), reviver);
|
||||
JS_FreeValue(js, holder);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static const JSCFunctionListEntry js_nota_funcs[] = {
|
||||
JS_CFUNC_DEF("encode", 1, js_nota_encode),
|
||||
JS_CFUNC_DEF("decode", 1, js_nota_decode),
|
||||
};
|
||||
|
||||
JSValue js_nota_use(JSContext *js) {
|
||||
JSValue export = JS_NewObject(js);
|
||||
JS_SetPropertyFunctionList(js, export, js_nota_funcs, sizeof(js_nota_funcs)/sizeof(JSCFunctionListEntry));
|
||||
return export;
|
||||
}
|
||||
@@ -577,8 +577,8 @@ static const JSCFunctionListEntry js_os_funcs[] = {
|
||||
|
||||
JSValue js_os_use(JSContext *js) {
|
||||
JS_NewClassID(&js_dylib_class_id);
|
||||
JS_NewClass(JS_GetRuntime(js), js_dylib_class_id, &js_dylib_class);
|
||||
|
||||
JS_NewClass(js, js_dylib_class_id, &js_dylib_class);
|
||||
|
||||
JSValue mod = JS_NewObject(js);
|
||||
JS_SetPropertyFunctionList(js,mod,js_os_funcs,countof(js_os_funcs));
|
||||
return mod;
|
||||
|
||||
439
internal/shop.cm
439
internal/shop.cm
@@ -5,7 +5,6 @@ var fd = use('fd')
|
||||
var http = use('http')
|
||||
var miniz = use('miniz')
|
||||
var time = use('time')
|
||||
var js = use('js')
|
||||
var crypto = use('crypto')
|
||||
var blob = use('blob')
|
||||
|
||||
@@ -13,6 +12,10 @@ var pkg_tools = use('package')
|
||||
var os = use('os')
|
||||
var link = use('link')
|
||||
|
||||
var analyze = os.analyze
|
||||
var run_ast_fn = os.run_ast_fn
|
||||
var shop_json = os.json
|
||||
|
||||
var core = "core"
|
||||
|
||||
function pull_from_cache(content)
|
||||
@@ -32,9 +35,10 @@ function ensure_dir(path) {
|
||||
if (fd.stat(path).isDirectory) return
|
||||
var parts = array(path, '/')
|
||||
var current = starts_with(path, '/') ? '/' : ''
|
||||
for (var i = 0; i < length(parts); i++) {
|
||||
var i = 0
|
||||
for (i = 0; i < length(parts); i++) {
|
||||
if (parts[i] == '') continue
|
||||
current += parts[i] + '/'
|
||||
current = current + parts[i] + '/'
|
||||
if (!fd.stat(current).isDirectory) {
|
||||
fd.mkdir(current)
|
||||
}
|
||||
@@ -76,12 +80,12 @@ function get_packages_dir() {
|
||||
}
|
||||
|
||||
// Get the core directory (in the global shop)
|
||||
var core_package = 'core'
|
||||
|
||||
Shop.get_core_dir = function() {
|
||||
return get_packages_dir() + '/' + core_package
|
||||
}
|
||||
|
||||
var core_package = 'core'
|
||||
|
||||
// Get the links file path (in the global shop)
|
||||
function get_links_path() {
|
||||
return global_shop_path + '/link.toml'
|
||||
@@ -116,12 +120,16 @@ function split_explicit_package_import(path)
|
||||
if (!looks_explicit) return null
|
||||
|
||||
// Find the longest prefix that is an installed package
|
||||
for (var i = length(parts) - 1; i >= 1; i--) {
|
||||
var pkg_candidate = text(array(parts, 0, i), '/')
|
||||
var mod_path = text(array(parts, i), '/')
|
||||
var i = 0
|
||||
var pkg_candidate = null
|
||||
var mod_path = null
|
||||
var candidate_dir = null
|
||||
for (i = length(parts) - 1; i >= 1; i--) {
|
||||
pkg_candidate = text(array(parts, 0, i), '/')
|
||||
mod_path = text(array(parts, i), '/')
|
||||
if (!mod_path || length(mod_path) == 0) continue
|
||||
|
||||
var candidate_dir = get_packages_dir() + '/' + safe_package_path(pkg_candidate)
|
||||
candidate_dir = get_packages_dir() + '/' + safe_package_path(pkg_candidate)
|
||||
if (fd.is_file(candidate_dir + '/cell.toml'))
|
||||
return {package: pkg_candidate, path: mod_path}
|
||||
|
||||
@@ -142,8 +150,10 @@ function package_in_shop(package) {
|
||||
|
||||
function abs_path_to_package(package_dir)
|
||||
{
|
||||
if (!fd.is_file(package_dir + '/cell.toml'))
|
||||
throw Error('Not a valid package directory (no cell.toml): ' + package_dir)
|
||||
if (!fd.is_file(package_dir + '/cell.toml')) {
|
||||
print('Not a valid package directory (no cell.toml): ' + package_dir)
|
||||
disrupt
|
||||
}
|
||||
|
||||
var packages_prefix = get_packages_dir() + '/'
|
||||
var core_dir = packages_prefix + core_package
|
||||
@@ -153,8 +163,9 @@ function abs_path_to_package(package_dir)
|
||||
return 'core'
|
||||
}
|
||||
// Also check if core_dir is a symlink pointing to package_dir
|
||||
var core_target = null
|
||||
if (fd.is_link(core_dir)) {
|
||||
var core_target = fd.readlink(core_dir)
|
||||
core_target = fd.readlink(core_dir)
|
||||
if (core_target == package_dir || fd.realpath(core_dir) == package_dir) {
|
||||
return 'core'
|
||||
}
|
||||
@@ -175,13 +186,14 @@ function abs_path_to_package(package_dir)
|
||||
return package_dir
|
||||
|
||||
// For local directories (e.g., linked targets), read the package name from cell.toml
|
||||
try {
|
||||
var content = text(fd.slurp(package_dir + '/cell.toml'))
|
||||
var cfg = toml.decode(content)
|
||||
var _toml_path = package_dir + '/cell.toml'
|
||||
var content = null
|
||||
var cfg = null
|
||||
if (fd.is_file(_toml_path)) {
|
||||
content = text(fd.slurp(_toml_path))
|
||||
cfg = toml.decode(content)
|
||||
if (cfg.package)
|
||||
return cfg.package
|
||||
} catch (e) {
|
||||
// Fall through
|
||||
}
|
||||
|
||||
return null
|
||||
@@ -299,23 +311,29 @@ Shop.resolve_package_info = function(pkg) {
|
||||
|
||||
// Verify if a package name is valid and return status
|
||||
Shop.verify_package_name = function(pkg) {
|
||||
if (!pkg) throw Error("Empty package name")
|
||||
if (pkg == 'local') throw Error("local is not a valid package name")
|
||||
if (pkg == 'core') throw Error("core is not a valid package name")
|
||||
|
||||
if (search(pkg, '://') != null)
|
||||
throw Error(`Invalid package name: ${pkg}; did you mean ${array(pkg, '://')[1]}?`)
|
||||
if (!pkg) { print("Empty package name"); disrupt }
|
||||
if (pkg == 'local') { print("local is not a valid package name"); disrupt }
|
||||
if (pkg == 'core') { print("core is not a valid package name"); disrupt }
|
||||
|
||||
if (search(pkg, '://') != null) {
|
||||
print(`Invalid package name: ${pkg}; did you mean ${array(pkg, '://')[1]}?`)
|
||||
disrupt
|
||||
}
|
||||
}
|
||||
|
||||
// Convert module package to download URL
|
||||
Shop.get_download_url = function(pkg, commit_hash) {
|
||||
var info = Shop.resolve_package_info(pkg)
|
||||
var parts = null
|
||||
var host = null
|
||||
var user = null
|
||||
var repo = null
|
||||
|
||||
if (info == 'gitea') {
|
||||
var parts = array(pkg, '/')
|
||||
var host = parts[0]
|
||||
var user = parts[1]
|
||||
var repo = parts[2]
|
||||
parts = array(pkg, '/')
|
||||
host = parts[0]
|
||||
user = parts[1]
|
||||
repo = parts[2]
|
||||
|
||||
return 'https://' + host + '/' + user + '/' + repo + '/archive/' + commit_hash + '.zip'
|
||||
}
|
||||
@@ -326,12 +344,16 @@ Shop.get_download_url = function(pkg, commit_hash) {
|
||||
// Get the API URL for checking remote git commits
|
||||
Shop.get_api_url = function(pkg) {
|
||||
var info = Shop.resolve_package_info(pkg)
|
||||
var parts = null
|
||||
var host = null
|
||||
var user = null
|
||||
var repo = null
|
||||
|
||||
if (info == 'gitea') {
|
||||
var parts = array(pkg, '/')
|
||||
var host = parts[0]
|
||||
var user = parts[1]
|
||||
var repo = parts[2]
|
||||
parts = array(pkg, '/')
|
||||
host = parts[0]
|
||||
user = parts[1]
|
||||
repo = parts[2]
|
||||
return 'https://' + host + '/api/v1/repos/' + user + '/' + repo + '/branches/'
|
||||
}
|
||||
|
||||
@@ -378,97 +400,105 @@ Shop.get_script_capabilities = function(path) {
|
||||
return Shop.script_inject_for(file_info)
|
||||
}
|
||||
|
||||
// Build the env object for a module, with runtime fns and $-prefixed capabilities.
|
||||
// Matches engine.cm's approach: env properties become free variables in the module.
|
||||
function inject_env(inject) {
|
||||
var env = {}
|
||||
for (var i = 0; i < length(inject); i++) {
|
||||
var inj = inject[i]
|
||||
var key = trim(inj, '$')
|
||||
if (key == 'fd') env[key] = fd
|
||||
else env[key] = my$_[key]
|
||||
var rt = my$_.os ? my$_.os.runtime_env : null
|
||||
if (rt) {
|
||||
arrfor(array(rt), function(k) { env[k] = rt[k] })
|
||||
}
|
||||
|
||||
// Add capability injections with $ prefix
|
||||
var i = 0
|
||||
var inj = null
|
||||
var key = null
|
||||
for (i = 0; i < length(inject); i++) {
|
||||
inj = inject[i]
|
||||
key = inj
|
||||
if (key && key[0] == '$') key = text(key, 1)
|
||||
if (key == 'fd') env['$fd'] = fd
|
||||
else env['$' + key] = my$_[key]
|
||||
}
|
||||
return env
|
||||
}
|
||||
|
||||
function inject_bindings_code(inject) {
|
||||
var lines = []
|
||||
for (var i = 0; i < length(inject); i++) {
|
||||
var inj = inject[i]
|
||||
var key = trim(inj, '$')
|
||||
push(lines, `var $${key} = env["${key}"];`)
|
||||
}
|
||||
return text(lines, '\n')
|
||||
}
|
||||
// Lazy-loaded compiler modules for on-the-fly compilation
|
||||
var _mcode_mod = null
|
||||
var _streamline_mod = null
|
||||
|
||||
// Build the use function for a specific package context
|
||||
function make_use_fn_code(pkg_arg) {
|
||||
return `function(path) { return globalThis.use(path, ${pkg_arg}); }`
|
||||
}
|
||||
|
||||
// for script forms, path is the canonical path of the module
|
||||
var script_form = function(path, script, pkg, inject) {
|
||||
var pkg_arg = pkg ? `'${pkg}'` : 'null'
|
||||
var binds = inject_bindings_code(inject)
|
||||
|
||||
var fn = `(function setup_module(args, use, env){
|
||||
def arg = args;
|
||||
def PACKAGE = ${pkg_arg};
|
||||
${binds}
|
||||
${script}
|
||||
})`
|
||||
return fn
|
||||
}
|
||||
|
||||
// Resolve module function, hashing it in the process
|
||||
// path is the exact path to the script file
|
||||
// Compile a module and return its bytecode blob.
|
||||
// The bytecode is cached on disk by content hash.
|
||||
function resolve_mod_fn(path, pkg) {
|
||||
if (!fd.is_file(path)) throw Error(`path ${path} is not a file`)
|
||||
if (!fd.is_file(path)) { print(`path ${path} is not a file`); disrupt }
|
||||
|
||||
var file_info = Shop.file_info(path)
|
||||
var file_pkg = file_info.package
|
||||
var inject = Shop.script_inject_for(file_info)
|
||||
var content = text(fd.slurp(path))
|
||||
var script = script_form(path, content, file_pkg, inject);
|
||||
|
||||
var obj = pull_from_cache(stone(blob(script)))
|
||||
if (obj) {
|
||||
var fn = js.compile_unblob(obj)
|
||||
return js.eval_compile(fn)
|
||||
var cached = pull_from_cache(stone(blob(content)))
|
||||
var ast = null
|
||||
var compiled = null
|
||||
var mach_path = null
|
||||
var mach_blob = null
|
||||
var ir = null
|
||||
var optimized = null
|
||||
|
||||
// Check cache for pre-compiled .mach blob
|
||||
if (cached) {
|
||||
return cached
|
||||
}
|
||||
|
||||
// Compile name is just for debug/stack traces
|
||||
// var compile_name = pkg ? pkg + ':' + path : 'local:' + path
|
||||
var compile_name = path
|
||||
|
||||
var fn = js.compile(compile_name, script)
|
||||
|
||||
put_into_cache(stone(blob(script)), js.compile_blob(fn))
|
||||
|
||||
return js.eval_compile(fn)
|
||||
|
||||
// Check for pre-compiled .mach file alongside .cm source
|
||||
if (ends_with(path, '.cm')) {
|
||||
mach_path = text(path, 0, length(path) - 3) + '.mach'
|
||||
if (fd.is_file(mach_path)) {
|
||||
mach_blob = fd.slurp(mach_path)
|
||||
put_into_cache(stone(blob(content)), mach_blob)
|
||||
return mach_blob
|
||||
}
|
||||
}
|
||||
|
||||
// Compile via full pipeline: analyze → mcode → streamline → serialize
|
||||
if (!_mcode_mod) _mcode_mod = Shop.use("mcode", null)
|
||||
if (!_streamline_mod) _streamline_mod = Shop.use("streamline", null)
|
||||
ast = analyze(content, path)
|
||||
ir = _mcode_mod(ast)
|
||||
optimized = _streamline_mod(ir)
|
||||
compiled = mach_compile_mcode_bin(path, shop_json.encode(optimized))
|
||||
put_into_cache(stone(blob(content)), compiled)
|
||||
|
||||
return compiled
|
||||
}
|
||||
|
||||
// given a path and a package context
|
||||
// return module info about where it was found
|
||||
function resolve_locator(path, ctx)
|
||||
{
|
||||
{
|
||||
var explicit = split_explicit_package_import(path)
|
||||
var explicit_path = null
|
||||
var fn = null
|
||||
var core_dir = null
|
||||
var core_file_path = null
|
||||
var is_core = null
|
||||
var scope = null
|
||||
var alias_path = null
|
||||
|
||||
if (explicit) {
|
||||
if (is_internal_path(explicit.path) && ctx && explicit.package != ctx)
|
||||
explicit = null
|
||||
}
|
||||
if (explicit) {
|
||||
var explicit_path = get_packages_dir() + '/' + safe_package_path(explicit.package) + '/' + explicit.path
|
||||
explicit_path = get_packages_dir() + '/' + safe_package_path(explicit.package) + '/' + explicit.path
|
||||
if (fd.is_file(explicit_path)) {
|
||||
var fn = resolve_mod_fn(explicit_path, explicit.package)
|
||||
fn = resolve_mod_fn(explicit_path, explicit.package)
|
||||
return {path: explicit_path, scope: SCOPE_PACKAGE, symbol: fn}
|
||||
}
|
||||
}
|
||||
|
||||
// 1. If no context, resolve from core only
|
||||
if (!ctx) {
|
||||
var core_dir = Shop.get_core_dir()
|
||||
var core_file_path = core_dir + '/' + path
|
||||
core_dir = Shop.get_core_dir()
|
||||
core_file_path = core_dir + '/' + path
|
||||
if (fd.is_file(core_file_path)) {
|
||||
var fn = resolve_mod_fn(core_file_path, 'core')
|
||||
fn = resolve_mod_fn(core_file_path, 'core')
|
||||
return {path: core_file_path, scope: SCOPE_CORE, symbol: fn}
|
||||
}
|
||||
return null
|
||||
@@ -477,7 +507,7 @@ function resolve_locator(path, ctx)
|
||||
// check in ctx package
|
||||
// If ctx is an absolute path (starts with /), use it directly
|
||||
// Otherwise, look it up in the packages directory
|
||||
var ctx_dir
|
||||
var ctx_dir = null
|
||||
if (starts_with(ctx, '/')) {
|
||||
ctx_dir = ctx
|
||||
} else {
|
||||
@@ -486,10 +516,10 @@ function resolve_locator(path, ctx)
|
||||
var ctx_path = ctx_dir + '/' + path
|
||||
|
||||
if (fd.is_file(ctx_path)) {
|
||||
var fn = resolve_mod_fn(ctx_path, ctx)
|
||||
fn = resolve_mod_fn(ctx_path, ctx)
|
||||
// Check if ctx is the core package (either by name or by path)
|
||||
var is_core = (ctx == 'core') || (ctx_dir == Shop.get_core_dir())
|
||||
var scope = is_core ? SCOPE_CORE : SCOPE_LOCAL
|
||||
is_core = (ctx == 'core') || (ctx_dir == Shop.get_core_dir())
|
||||
scope = is_core ? SCOPE_CORE : SCOPE_LOCAL
|
||||
return {path: ctx_path, scope: scope, symbol: fn}
|
||||
}
|
||||
|
||||
@@ -499,24 +529,24 @@ function resolve_locator(path, ctx)
|
||||
// check for aliased dependency
|
||||
var alias = pkg_tools.split_alias(ctx, path)
|
||||
if (alias) {
|
||||
var alias_path = get_packages_dir() + '/' + safe_package_path(alias.package) + '/' + alias.path
|
||||
alias_path = get_packages_dir() + '/' + safe_package_path(alias.package) + '/' + alias.path
|
||||
if (fd.is_file(alias_path)) {
|
||||
var fn = resolve_mod_fn(alias_path, ctx)
|
||||
fn = resolve_mod_fn(alias_path, ctx)
|
||||
return {path: alias_path, scope:SCOPE_PACKAGE, symbol:fn}
|
||||
}
|
||||
}
|
||||
|
||||
var package_path = get_packages_dir() + '/' + safe_package_path(path)
|
||||
var package_path = get_packages_dir() + '/' + safe_package_path(path)
|
||||
if (fd.is_file(package_path)) {
|
||||
var fn = resolve_mod_fn(package_path, ctx)
|
||||
fn = resolve_mod_fn(package_path, ctx)
|
||||
return {path: package_path, scope: SCOPE_PACKAGE, symbol: fn}
|
||||
}
|
||||
|
||||
// 4. Check core as fallback
|
||||
var core_dir = Shop.get_core_dir()
|
||||
var core_file_path = core_dir + '/' + path
|
||||
core_dir = Shop.get_core_dir()
|
||||
core_file_path = core_dir + '/' + path
|
||||
if (fd.is_file(core_file_path)) {
|
||||
var fn = resolve_mod_fn(core_file_path, 'core')
|
||||
fn = resolve_mod_fn(core_file_path, 'core')
|
||||
return {path: core_file_path, scope: SCOPE_CORE, symbol: fn}
|
||||
}
|
||||
|
||||
@@ -548,7 +578,7 @@ Shop.open_package_dylib = function(pkg) {
|
||||
var link_target = link.get_target(pkg)
|
||||
var resolved_pkg = link_target ? link_target : pkg
|
||||
|
||||
var pkg_dir;
|
||||
var pkg_dir = null
|
||||
if (starts_with(resolved_pkg, '/')) {
|
||||
pkg_dir = resolved_pkg
|
||||
} else {
|
||||
@@ -556,34 +586,23 @@ Shop.open_package_dylib = function(pkg) {
|
||||
}
|
||||
|
||||
var toml_path = pkg_dir + '/cell.toml'
|
||||
var content = null
|
||||
var cfg = null
|
||||
if (fd.is_file(toml_path)) {
|
||||
try {
|
||||
var content = text(fd.slurp(toml_path))
|
||||
var cfg = toml.decode(content)
|
||||
if (cfg.dependencies) {
|
||||
arrfor(array(cfg.dependencies), function(alias, i) {
|
||||
var dep_pkg = cfg.dependencies[alias]
|
||||
try {
|
||||
Shop.open_package_dylib(dep_pkg)
|
||||
} catch (dep_e) {
|
||||
// Dependency dylib load failed, continue with others
|
||||
}
|
||||
})
|
||||
}
|
||||
} catch (e) {
|
||||
// Error reading toml, continue
|
||||
content = text(fd.slurp(toml_path))
|
||||
cfg = toml.decode(content)
|
||||
if (cfg.dependencies) {
|
||||
arrfor(array(cfg.dependencies), function(alias, i) {
|
||||
var dep_pkg = cfg.dependencies[alias]
|
||||
Shop.open_package_dylib(dep_pkg)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
var dl_path = get_lib_path(pkg)
|
||||
if (fd.is_file(dl_path)) {
|
||||
if (!open_dls[dl_path]) {
|
||||
try {
|
||||
open_dls[dl_path] = os.dylib_open(dl_path)
|
||||
} catch (e) {
|
||||
dylib_visited[pkg] = false
|
||||
throw e
|
||||
}
|
||||
open_dls[dl_path] = os.dylib_open(dl_path)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -594,12 +613,19 @@ Shop.open_package_dylib = function(pkg) {
|
||||
// Core is never loaded as a dynamic library via dlopen
|
||||
function resolve_c_symbol(path, package_context) {
|
||||
var explicit = split_explicit_package_import(path)
|
||||
var sym = null
|
||||
var dl_path = null
|
||||
var _path = null
|
||||
var core_sym = null
|
||||
var canon_pkg = null
|
||||
var mod_name = null
|
||||
|
||||
if (explicit) {
|
||||
if (is_internal_path(explicit.path) && package_context && explicit.package != package_context)
|
||||
explicit = null
|
||||
}
|
||||
if (explicit) {
|
||||
var sym = make_c_symbol(explicit.package, explicit.path)
|
||||
sym = make_c_symbol(explicit.package, explicit.path)
|
||||
if (os.internal_exists(sym)) {
|
||||
return {
|
||||
symbol: function() { return os.load_internal(sym) },
|
||||
@@ -610,7 +636,7 @@ function resolve_c_symbol(path, package_context) {
|
||||
}
|
||||
|
||||
Shop.open_package_dylib(explicit.package)
|
||||
var dl_path = get_lib_path(explicit.package)
|
||||
dl_path = get_lib_path(explicit.package)
|
||||
if (open_dls[dl_path] && os.dylib_has_symbol(open_dls[dl_path], sym)) {
|
||||
return {
|
||||
symbol: function() { return os.dylib_symbol(open_dls[dl_path], sym) },
|
||||
@@ -623,8 +649,8 @@ function resolve_c_symbol(path, package_context) {
|
||||
|
||||
// If no package context, only check core internal symbols
|
||||
if (!package_context || package_context == 'core') {
|
||||
path = replace(path, '/', '_')
|
||||
var core_sym = `js_${path}_use`
|
||||
_path = replace(path, '/', '_')
|
||||
core_sym = `js_${_path}_use`
|
||||
if (os.internal_exists(core_sym)) {
|
||||
return {
|
||||
symbol: function() { return os.load_internal(core_sym) },
|
||||
@@ -636,7 +662,7 @@ function resolve_c_symbol(path, package_context) {
|
||||
}
|
||||
|
||||
// 1. Check own package first (internal, then dylib)
|
||||
var sym = make_c_symbol(package_context, path)
|
||||
sym = make_c_symbol(package_context, path)
|
||||
if (os.internal_exists(sym)) {
|
||||
return {
|
||||
symbol: function() { return os.load_internal(sym) },
|
||||
@@ -646,7 +672,7 @@ function resolve_c_symbol(path, package_context) {
|
||||
}
|
||||
|
||||
Shop.open_package_dylib(package_context)
|
||||
var dl_path = get_lib_path(package_context)
|
||||
dl_path = get_lib_path(package_context)
|
||||
|
||||
if (open_dls[dl_path] && os.dylib_has_symbol(open_dls[dl_path], sym)) {
|
||||
return {
|
||||
@@ -662,10 +688,10 @@ function resolve_c_symbol(path, package_context) {
|
||||
// 2. Check aliased package imports (e.g. 'prosperon/sprite')
|
||||
var pkg_alias = get_import_package(path)
|
||||
if (pkg_alias) {
|
||||
var canon_pkg = get_aliased_package(path, package_context)
|
||||
canon_pkg = get_aliased_package(path, package_context)
|
||||
if (canon_pkg) {
|
||||
var mod_name = get_import_name(path)
|
||||
var sym = make_c_symbol(canon_pkg, mod_name)
|
||||
mod_name = get_import_name(path)
|
||||
sym = make_c_symbol(canon_pkg, mod_name)
|
||||
|
||||
// Check internal first
|
||||
if (os.internal_exists(sym)) {
|
||||
@@ -679,7 +705,7 @@ function resolve_c_symbol(path, package_context) {
|
||||
|
||||
// Then check dylib
|
||||
Shop.open_package_dylib(canon_pkg)
|
||||
var dl_path = get_lib_path(canon_pkg)
|
||||
dl_path = get_lib_path(canon_pkg)
|
||||
if (open_dls[dl_path] && os.dylib_has_symbol(open_dls[dl_path], sym)) {
|
||||
return {
|
||||
symbol: function() { return os.dylib_symbol(open_dls[dl_path], sym) },
|
||||
@@ -692,7 +718,7 @@ function resolve_c_symbol(path, package_context) {
|
||||
}
|
||||
|
||||
// 3. Check core internal symbols (core is never a dynamic library)
|
||||
var core_sym = `js_${replace(path, '/', '_')}_use`
|
||||
core_sym = `js_${replace(path, '/', '_')}_use`
|
||||
if (os.internal_exists(core_sym)) {
|
||||
return {
|
||||
symbol: function() { return os.load_internal(core_sym) },
|
||||
@@ -720,31 +746,37 @@ function resolve_module_info(path, package_context) {
|
||||
if (min_scope == 999)
|
||||
return null
|
||||
|
||||
var cache_key
|
||||
var cache_key = null
|
||||
var real_path = null
|
||||
var real_info = null
|
||||
var pkg_alias = null
|
||||
var canon_pkg = null
|
||||
var mod_name = null
|
||||
|
||||
if (mod_resolve.scope == SCOPE_CORE) {
|
||||
cache_key = 'core/' + path
|
||||
} else if (mod_resolve.scope < 900 && mod_resolve.path) {
|
||||
var real_path = fd.realpath(mod_resolve.path)
|
||||
real_path = fd.realpath(mod_resolve.path)
|
||||
if (real_path) {
|
||||
var real_info = Shop.file_info(real_path)
|
||||
real_info = Shop.file_info(real_path)
|
||||
if (real_info.package && real_info.name)
|
||||
cache_key = real_info.package + '/' + real_info.name
|
||||
else
|
||||
cache_key = real_path
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (!cache_key) {
|
||||
if (min_scope == SCOPE_CORE)
|
||||
cache_key = 'core/' + path
|
||||
else if (min_scope == SCOPE_LOCAL && package_context)
|
||||
cache_key = package_context + '/' + path
|
||||
else if (min_scope == SCOPE_PACKAGE) {
|
||||
var pkg_alias = get_import_package(path)
|
||||
pkg_alias = get_import_package(path)
|
||||
if (pkg_alias) {
|
||||
var canon_pkg = get_canonical_package(pkg_alias, package_context)
|
||||
canon_pkg = get_canonical_package(pkg_alias, package_context)
|
||||
if (canon_pkg) {
|
||||
var mod_name = get_import_name(path)
|
||||
mod_name = get_import_name(path)
|
||||
cache_key = canon_pkg + '/' + mod_name
|
||||
} else
|
||||
cache_key = path
|
||||
@@ -795,54 +827,50 @@ function execute_module(info)
|
||||
var c_resolve = info.c_resolve
|
||||
var mod_resolve = info.mod_resolve
|
||||
|
||||
var used
|
||||
var used = null
|
||||
var file_info = null
|
||||
var inject = null
|
||||
var env = null
|
||||
var pkg = null
|
||||
|
||||
if (mod_resolve.scope < 900) {
|
||||
var context = null
|
||||
// Build env with runtime fns, capabilities, and use function
|
||||
file_info = Shop.file_info(mod_resolve.path)
|
||||
inject = Shop.script_inject_for(file_info)
|
||||
env = inject_env(inject)
|
||||
pkg = file_info.package
|
||||
env.use = make_use_fn(pkg)
|
||||
|
||||
// Add C module as native context if available
|
||||
if (c_resolve.scope < 900) {
|
||||
context = call_c_module(c_resolve)
|
||||
env.native = call_c_module(c_resolve)
|
||||
}
|
||||
|
||||
// Get file info to determine inject list
|
||||
var file_info = Shop.file_info(mod_resolve.path)
|
||||
var inject = Shop.script_inject_for(file_info)
|
||||
var env = inject_env(inject)
|
||||
var pkg = file_info.package
|
||||
var use_fn = make_use_fn(pkg)
|
||||
|
||||
// Call with signature: setup_module(args, use, env)
|
||||
// args is null for module loading
|
||||
used = call(mod_resolve.symbol, context, [null, use_fn, env])
|
||||
// Load compiled bytecode with env
|
||||
used = mach_load(mod_resolve.symbol, env)
|
||||
} else if (c_resolve.scope < 900) {
|
||||
// C only
|
||||
used = call_c_module(c_resolve)
|
||||
} else {
|
||||
throw Error(`Module ${info.path} could not be found`)
|
||||
print(`Module ${info.path} could not be found`); disrupt
|
||||
}
|
||||
|
||||
// if (is_function(used))
|
||||
// throw Error('C module loader returned a function; did you forget to call it?')
|
||||
if (!used) { print(`Module ${info} returned null`); disrupt }
|
||||
|
||||
if (!used)
|
||||
throw Error(`Module ${info} returned null`)
|
||||
|
||||
// stone(used)
|
||||
return used
|
||||
}
|
||||
|
||||
function get_module(path, package_context) {
|
||||
var info = resolve_module_info(path, package_context)
|
||||
|
||||
if (!info)
|
||||
throw Error(`Module ${path} could not be found in ${package_context}`)
|
||||
if (!info) { print(`Module ${path} could not be found in ${package_context}`); disrupt }
|
||||
|
||||
return execute_module(info)
|
||||
}
|
||||
|
||||
Shop.use = function use(path, package_context) {
|
||||
var info = resolve_module_info(path, package_context)
|
||||
if (!info)
|
||||
throw Error(`Module ${path} could not be found in ${package_context}`)
|
||||
if (!info) { print(`Module ${path} could not be found in ${package_context}`); disrupt }
|
||||
|
||||
if (use_cache[info.cache_key])
|
||||
return use_cache[info.cache_key]
|
||||
@@ -870,13 +898,13 @@ function fetch_remote_hash(pkg) {
|
||||
if (!api_url) return null
|
||||
|
||||
|
||||
try {
|
||||
var _fetch_hash = function() {
|
||||
var resp = http.fetch(api_url)
|
||||
return Shop.extract_commit_hash(pkg, text(resp))
|
||||
} catch (e) {
|
||||
log.console("Warning: Could not check for updates for " + pkg)
|
||||
} disruption {
|
||||
return null
|
||||
}
|
||||
return _fetch_hash()
|
||||
}
|
||||
|
||||
// Download a zip for a package at a specific commit and cache it
|
||||
@@ -890,14 +918,14 @@ function download_zip(pkg, commit_hash) {
|
||||
return null
|
||||
}
|
||||
|
||||
try {
|
||||
var _download = function() {
|
||||
var zip_blob = http.fetch(download_url)
|
||||
fd.slurpwrite(cache_path, zip_blob)
|
||||
return zip_blob
|
||||
} catch (e) {
|
||||
log.error("Download failed for " + pkg + ": " + e)
|
||||
} disruption {
|
||||
return null
|
||||
}
|
||||
return _download()
|
||||
}
|
||||
|
||||
// Get zip from cache, returns null if not cached
|
||||
@@ -933,17 +961,18 @@ Shop.fetch = function(pkg) {
|
||||
// Check if we have the zip cached
|
||||
var zip_blob = get_cached_zip(pkg, commit)
|
||||
|
||||
var actual_hash = null
|
||||
if (zip_blob) {
|
||||
// If we have a hash on record, verify it
|
||||
if (expected_hash) {
|
||||
var actual_hash = text(crypto.blake2(zip_blob), 'h')
|
||||
actual_hash = text(crypto.blake2(zip_blob), 'h')
|
||||
if (actual_hash == expected_hash) {
|
||||
return { status: 'cached' }
|
||||
}
|
||||
log.console("Zip hash mismatch for " + pkg + ", re-fetching...")
|
||||
} else {
|
||||
// No hash stored yet - compute and store it
|
||||
var actual_hash = text(crypto.blake2(zip_blob), 'h')
|
||||
actual_hash = text(crypto.blake2(zip_blob), 'h')
|
||||
lock_entry.zip_hash = actual_hash
|
||||
Shop.save_lock(lock)
|
||||
return { status: 'cached' }
|
||||
@@ -995,10 +1024,12 @@ Shop.extract = function(pkg) {
|
||||
// Check if already extracted at correct commit
|
||||
var lock = Shop.load_lock()
|
||||
var lock_entry = lock[pkg]
|
||||
var extracted_commit_file = null
|
||||
var extracted_commit = null
|
||||
if (lock_entry && lock_entry.commit) {
|
||||
var extracted_commit_file = target_dir + '/.cell_commit'
|
||||
extracted_commit_file = target_dir + '/.cell_commit'
|
||||
if (fd.is_file(extracted_commit_file)) {
|
||||
var extracted_commit = trim(text(fd.slurp(extracted_commit_file)))
|
||||
extracted_commit = trim(text(fd.slurp(extracted_commit_file)))
|
||||
if (extracted_commit == lock_entry.commit) {
|
||||
// Already extracted at this commit, skip
|
||||
return true
|
||||
@@ -1009,7 +1040,7 @@ Shop.extract = function(pkg) {
|
||||
var zip_blob = get_package_zip(pkg)
|
||||
|
||||
if (!zip_blob)
|
||||
throw Error("No zip blob available for " + pkg)
|
||||
print("No zip blob available for " + pkg); disrupt
|
||||
|
||||
// Extract zip for remote package
|
||||
install_zip(zip_blob, target_dir)
|
||||
@@ -1050,6 +1081,7 @@ Shop.update = function(pkg) {
|
||||
|
||||
log.console(`checking ${pkg}`)
|
||||
|
||||
var new_entry = null
|
||||
if (info == 'local') {
|
||||
// Check if local path exists
|
||||
if (!fd.is_dir(pkg)) {
|
||||
@@ -1057,7 +1089,7 @@ Shop.update = function(pkg) {
|
||||
return null
|
||||
}
|
||||
// Local packages always get a lock entry
|
||||
var new_entry = {
|
||||
new_entry = {
|
||||
type: 'local',
|
||||
updated: time.number()
|
||||
}
|
||||
@@ -1080,7 +1112,7 @@ Shop.update = function(pkg) {
|
||||
if (local_commit == remote_commit)
|
||||
return null
|
||||
|
||||
var new_entry = {
|
||||
new_entry = {
|
||||
type: info,
|
||||
commit: remote_commit,
|
||||
updated: time.number()
|
||||
@@ -1094,7 +1126,7 @@ Shop.update = function(pkg) {
|
||||
|
||||
function install_zip(zip_blob, target_dir) {
|
||||
var zip = miniz.read(zip_blob)
|
||||
if (!zip) throw Error("Failed to read zip archive")
|
||||
if (!zip) { print("Failed to read zip archive"); disrupt }
|
||||
|
||||
if (fd.is_link(target_dir)) fd.unlink(target_dir)
|
||||
if (fd.is_dir(target_dir)) fd.rmdir(target_dir, 1)
|
||||
@@ -1105,21 +1137,28 @@ function install_zip(zip_blob, target_dir) {
|
||||
var count = zip.count()
|
||||
var created_dirs = {}
|
||||
|
||||
for (var i = 0; i < count; i++) {
|
||||
var i = 0
|
||||
var filename = null
|
||||
var slash_pos = null
|
||||
var rel_path = null
|
||||
var full_path = null
|
||||
var dir_path = null
|
||||
var file_data = null
|
||||
for (i = 0; i < count; i++) {
|
||||
if (zip.is_directory(i)) continue
|
||||
var filename = zip.get_filename(i)
|
||||
var slash_pos = search(filename, '/')
|
||||
filename = zip.get_filename(i)
|
||||
slash_pos = search(filename, '/')
|
||||
if (slash_pos == null) continue
|
||||
if (slash_pos + 1 >= length(filename)) continue
|
||||
var rel_path = text(filename, slash_pos + 1)
|
||||
var full_path = target_dir + '/' + rel_path
|
||||
var dir_path = fd.dirname(full_path)
|
||||
rel_path = text(filename, slash_pos + 1)
|
||||
full_path = target_dir + '/' + rel_path
|
||||
dir_path = fd.dirname(full_path)
|
||||
|
||||
if (!created_dirs[dir_path]) {
|
||||
ensure_dir(dir_path)
|
||||
created_dirs[dir_path] = true
|
||||
}
|
||||
var file_data = zip.slurp(filename)
|
||||
file_data = zip.slurp(filename)
|
||||
|
||||
stone(file_data)
|
||||
|
||||
@@ -1142,18 +1181,20 @@ Shop.remove = function(pkg) {
|
||||
|
||||
Shop.get = function(pkg) {
|
||||
var lock = Shop.load_lock()
|
||||
|
||||
var info = null
|
||||
var commit = null
|
||||
|
||||
if (!lock[pkg]) {
|
||||
var info = Shop.resolve_package_info(pkg)
|
||||
info = Shop.resolve_package_info(pkg)
|
||||
if (!info) {
|
||||
throw Error("Invalid package: " + pkg)
|
||||
print("Invalid package: " + pkg); disrupt
|
||||
}
|
||||
|
||||
var commit = null
|
||||
|
||||
commit = null
|
||||
if (info != 'local') {
|
||||
commit = fetch_remote_hash(pkg)
|
||||
if (!commit) {
|
||||
throw Error("Could not resolve commit for " + pkg)
|
||||
print("Could not resolve commit for " + pkg); disrupt
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1169,8 +1210,6 @@ Shop.get = function(pkg) {
|
||||
// Compile a module
|
||||
// List all files in a package
|
||||
|
||||
var debug = use('debug')
|
||||
|
||||
Shop.file_reload = function(file)
|
||||
{
|
||||
var info = Shop.file_info(file)
|
||||
@@ -1209,9 +1248,11 @@ function get_package_scripts(package)
|
||||
{
|
||||
var files = pkg_tools.list_files(package)
|
||||
var scripts = []
|
||||
|
||||
for (var i = 0; i < length(files); i++) {
|
||||
var file = files[i]
|
||||
|
||||
var i = 0
|
||||
var file = null
|
||||
for (i = 0; i < length(files); i++) {
|
||||
file = files[i]
|
||||
if (ends_with(file, '.cm') || ends_with(file, '.ce')) {
|
||||
push(scripts, file)
|
||||
}
|
||||
|
||||
@@ -4,19 +4,21 @@ var pkg = use('package')
|
||||
|
||||
// Check if current directory is a valid cell package
|
||||
function is_valid_package(dir) {
|
||||
if (!dir) dir = '.'
|
||||
return fd.is_file(dir + '/cell.toml')
|
||||
var _dir = dir == null ? '.' : dir
|
||||
if (!_dir) _dir = '.'
|
||||
return fd.is_file(_dir + '/cell.toml')
|
||||
}
|
||||
|
||||
// Get current package name from cell.toml or null
|
||||
function get_current_package_name() {
|
||||
if (!is_valid_package('.')) return null
|
||||
try {
|
||||
var _load = function() {
|
||||
var config = pkg.load_config(null)
|
||||
return config.package || 'local'
|
||||
} catch (e) {
|
||||
} disruption {
|
||||
return 'local'
|
||||
}
|
||||
return _load()
|
||||
}
|
||||
|
||||
// Get the directory for a package
|
||||
@@ -37,9 +39,10 @@ function ensure_dir(path) {
|
||||
|
||||
var parts = array(path, '/')
|
||||
var current = starts_with(path, '/') ? '/' : ''
|
||||
for (var i = 0; i < length(parts); i++) {
|
||||
var i = 0
|
||||
for (i = 0; i < length(parts); i++) {
|
||||
if (parts[i] == '') continue
|
||||
current += parts[i] + '/'
|
||||
current = current + parts[i] + '/'
|
||||
if (!fd.is_dir(current)) {
|
||||
fd.mkdir(current)
|
||||
}
|
||||
|
||||
74
link.cm
74
link.cm
@@ -34,9 +34,10 @@ function ensure_dir(path) {
|
||||
if (fd.stat(path).isDirectory) return
|
||||
var parts = array(path, '/')
|
||||
var current = starts_with(path, '/') ? '/' : ''
|
||||
for (var i = 0; i < length(parts); i++) {
|
||||
var i = 0
|
||||
for (i = 0; i < length(parts); i++) {
|
||||
if (parts[i] == '') continue
|
||||
current += parts[i] + '/'
|
||||
current = current + parts[i] + '/'
|
||||
if (!fd.stat(current).isDirectory) {
|
||||
fd.mkdir(current)
|
||||
}
|
||||
@@ -66,14 +67,16 @@ Link.load = function() {
|
||||
return link_cache
|
||||
}
|
||||
|
||||
try {
|
||||
var _load = function() {
|
||||
var content = text(fd.slurp(path))
|
||||
var cfg = toml.decode(content)
|
||||
link_cache = cfg.links || {}
|
||||
} catch (e) {
|
||||
log.console("Warning: Failed to load link.toml: " + e)
|
||||
if (cfg && cfg.links) link_cache = cfg.links
|
||||
else link_cache = {}
|
||||
} disruption {
|
||||
print("Warning: Failed to load link.toml\n")
|
||||
link_cache = {}
|
||||
}
|
||||
_load()
|
||||
return link_cache
|
||||
}
|
||||
|
||||
@@ -90,14 +93,16 @@ Link.add = function(canonical, target, shop) {
|
||||
// Validate canonical package exists in shop
|
||||
var lock = shop.load_lock()
|
||||
if (!lock[canonical]) {
|
||||
throw Error('Package ' + canonical + ' is not installed. Install it first with: cell get ' + canonical)
|
||||
print('Package ' + canonical + ' is not installed. Install it first with: cell get ' + canonical + '\n')
|
||||
disrupt
|
||||
}
|
||||
|
||||
// Validate target is a valid package
|
||||
if (starts_with(target, '/')) {
|
||||
// Local path - must have cell.toml
|
||||
if (!fd.is_file(target + '/cell.toml')) {
|
||||
throw Error('Target ' + target + ' is not a valid package (no cell.toml)')
|
||||
print('Target ' + target + ' is not a valid package (no cell.toml)\n')
|
||||
disrupt
|
||||
}
|
||||
} else {
|
||||
// Remote package target - ensure it's installed
|
||||
@@ -115,34 +120,36 @@ Link.add = function(canonical, target, shop) {
|
||||
// Read the target's cell.toml to find its dependencies
|
||||
var target_path = starts_with(target, '/') ? target : get_package_abs_dir(target)
|
||||
var toml_path = target_path + '/cell.toml'
|
||||
var _install_deps = null
|
||||
if (fd.is_file(toml_path)) {
|
||||
try {
|
||||
_install_deps = function() {
|
||||
var content = text(fd.slurp(toml_path))
|
||||
var cfg = toml.decode(content)
|
||||
if (cfg.dependencies) {
|
||||
if (cfg && cfg.dependencies) {
|
||||
arrfor(array(cfg.dependencies), function(alias) {
|
||||
var dep_locator = cfg.dependencies[alias]
|
||||
// Skip local dependencies that don't exist
|
||||
if (starts_with(dep_locator, '/') && !fd.is_dir(dep_locator)) {
|
||||
log.console(" Skipping missing local dependency: " + dep_locator)
|
||||
print(" Skipping missing local dependency: " + dep_locator + "\n")
|
||||
return
|
||||
}
|
||||
// Install the dependency if not already in shop
|
||||
try {
|
||||
var _get_dep = function() {
|
||||
shop.get(dep_locator)
|
||||
shop.extract(dep_locator)
|
||||
} catch (e) {
|
||||
log.console(` Warning: Could not install dependency ${dep_locator}: ${e.message}`)
|
||||
log.error(e)
|
||||
} disruption {
|
||||
print(` Warning: Could not install dependency ${dep_locator}\n`)
|
||||
}
|
||||
_get_dep()
|
||||
})
|
||||
}
|
||||
} catch (e) {
|
||||
log.console(` Warning: Could not read dependencies from ${toml_path}`)
|
||||
} disruption {
|
||||
print(` Warning: Could not read dependencies from ${toml_path}\n`)
|
||||
}
|
||||
_install_deps()
|
||||
}
|
||||
|
||||
log.console("Linked " + canonical + " -> " + target)
|
||||
print("Linked " + canonical + " -> " + target + "\n")
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -154,12 +161,12 @@ Link.remove = function(canonical) {
|
||||
var target_dir = get_package_abs_dir(canonical)
|
||||
if (fd.is_link(target_dir)) {
|
||||
fd.unlink(target_dir)
|
||||
log.console("Removed symlink at " + target_dir)
|
||||
print("Removed symlink at " + target_dir + "\n")
|
||||
}
|
||||
|
||||
|
||||
delete links[canonical]
|
||||
Link.save(links)
|
||||
log.console("Unlinked " + canonical)
|
||||
print("Unlinked " + canonical + "\n")
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -174,7 +181,7 @@ Link.clear = function() {
|
||||
})
|
||||
|
||||
Link.save({})
|
||||
log.console("Cleared all links")
|
||||
print("Cleared all links\n")
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -218,7 +225,7 @@ Link.sync_all = function(shop) {
|
||||
|
||||
arrfor(array(links), function(canonical) {
|
||||
var target = links[canonical]
|
||||
try {
|
||||
var _sync = function() {
|
||||
// Validate target exists
|
||||
var link_target = resolve_link_target(target)
|
||||
if (!fd.is_dir(link_target)) {
|
||||
@@ -234,10 +241,10 @@ Link.sync_all = function(shop) {
|
||||
|
||||
// Install dependencies of the linked package
|
||||
var toml_path = link_target + '/cell.toml'
|
||||
try {
|
||||
var _install = function() {
|
||||
var content = text(fd.slurp(toml_path))
|
||||
var cfg = toml.decode(content)
|
||||
if (cfg.dependencies) {
|
||||
if (cfg && cfg.dependencies) {
|
||||
arrfor(array(cfg.dependencies), function(alias) {
|
||||
var dep_locator = cfg.dependencies[alias]
|
||||
// Skip local dependencies that don't exist
|
||||
@@ -245,22 +252,25 @@ Link.sync_all = function(shop) {
|
||||
return
|
||||
}
|
||||
// Install the dependency if not already in shop
|
||||
try {
|
||||
var _get = function() {
|
||||
shop.get(dep_locator)
|
||||
shop.extract(dep_locator)
|
||||
} catch (e) {
|
||||
} disruption {
|
||||
// Silently continue - dependency may already be installed
|
||||
}
|
||||
_get()
|
||||
})
|
||||
}
|
||||
} catch (e) {
|
||||
} disruption {
|
||||
// Could not read dependencies - continue anyway
|
||||
}
|
||||
_install()
|
||||
|
||||
count++
|
||||
} catch (e) {
|
||||
push(errors, canonical + ': ' + e.message)
|
||||
count = count + 1
|
||||
} disruption {
|
||||
push(errors, canonical + ': sync failed')
|
||||
}
|
||||
_sync()
|
||||
})
|
||||
|
||||
return { synced: count, errors: errors }
|
||||
@@ -269,7 +279,7 @@ Link.sync_all = function(shop) {
|
||||
// Check if a package is currently linked
|
||||
Link.is_linked = function(canonical) {
|
||||
var links = Link.load()
|
||||
return canonical in links
|
||||
return links[canonical] != null
|
||||
}
|
||||
|
||||
// Get the link target for a package (or null if not linked)
|
||||
|
||||
13
mcode.ce
Normal file
13
mcode.ce
Normal file
@@ -0,0 +1,13 @@
|
||||
var fd = use("fd")
|
||||
var json = use("json")
|
||||
var tokenize = use("tokenize")
|
||||
var parse = use("parse")
|
||||
var fold = use("fold")
|
||||
var mcode = use("mcode")
|
||||
var filename = args[0]
|
||||
var src = text(fd.slurp(filename))
|
||||
var result = tokenize(src, filename)
|
||||
var ast = parse(result.tokens, src, filename, tokenize)
|
||||
var folded = fold(ast)
|
||||
var compiled = mcode(folded)
|
||||
print(json.encode(compiled))
|
||||
BIN
mcode.mach
Normal file
BIN
mcode.mach
Normal file
Binary file not shown.
@@ -43,16 +43,16 @@ src += [ # core
|
||||
'suite.c',
|
||||
'wildmatch.c',
|
||||
'qjs_actor.c',
|
||||
'qjs_wota.c',
|
||||
'miniz.c',
|
||||
'quickjs.c',
|
||||
'runtime.c',
|
||||
'mach.c',
|
||||
'libregexp.c', 'libunicode.c', 'cutils.c', 'dtoa.c'
|
||||
]
|
||||
|
||||
src += ['scheduler.c']
|
||||
src += ['qbe_helpers.c']
|
||||
|
||||
scripts = [
|
||||
'internal/nota.c',
|
||||
'debug/js.c',
|
||||
'qop.c',
|
||||
'wildstar.c',
|
||||
@@ -60,7 +60,6 @@ scripts = [
|
||||
'crypto.c',
|
||||
'internal/kim.c',
|
||||
'time.c',
|
||||
'internal/nota.c',
|
||||
'debug/debug.c',
|
||||
'internal/os.c',
|
||||
'fd.c',
|
||||
@@ -68,6 +67,7 @@ scripts = [
|
||||
'net/enet.c',
|
||||
'wildstar.c',
|
||||
'archive/miniz.c',
|
||||
'source/cJSON.c'
|
||||
]
|
||||
|
||||
foreach file: scripts
|
||||
|
||||
18
net/enet.c
18
net/enet.c
@@ -14,11 +14,11 @@ static void js_enet_host_finalizer(JSRuntime *rt, JSValue val)
|
||||
if (host) enet_host_destroy(host);
|
||||
}
|
||||
|
||||
static void js_enet_peer_mark(JSRuntime *rt, JSValueConst val, JS_MarkFunc *mark_func)
|
||||
{
|
||||
ENetPeer *peer = JS_GetOpaque(val, enet_peer_class_id);
|
||||
JS_MarkValue(rt, *(JSValue*)peer->data, mark_func);
|
||||
}
|
||||
//static void js_enet_peer_mark(JSRuntime *rt, JSValueConst val, JS_MarkFunc *mark_func)
|
||||
//{
|
||||
// ENetPeer *peer = JS_GetOpaque(val, enet_peer_class_id);
|
||||
// JS_MarkValue(rt, *(JSValue*)peer->data, mark_func);
|
||||
//}
|
||||
|
||||
static void js_enet_peer_finalizer(JSRuntime *rt, JSValue val)
|
||||
{
|
||||
@@ -62,7 +62,7 @@ static JSValue js_enet_host_create(JSContext *ctx, JSValueConst this_val, int ar
|
||||
enet_uint32 outgoing_bandwidth = 0;
|
||||
JSValue obj;
|
||||
|
||||
if (argc < 1 || !JS_IsObject(argv[0])) {
|
||||
if (argc < 1 || !JS_IsRecord(argv[0])) {
|
||||
host = enet_host_create(NULL, peer_count, channel_limit, incoming_bandwidth, outgoing_bandwidth);
|
||||
if (!host) return JS_ThrowInternalError(ctx, "Failed to create ENet client host");
|
||||
goto wrap;
|
||||
@@ -414,7 +414,7 @@ static JSClassDef enet_host = {
|
||||
static JSClassDef enet_peer_class = {
|
||||
"ENetPeer",
|
||||
.finalizer = js_enet_peer_finalizer,
|
||||
.gc_mark = js_enet_peer_mark
|
||||
// .gc_mark = js_enet_peer_mark
|
||||
};
|
||||
|
||||
JSValue js_enet_resolve_hostname(JSContext *js, JSValue self, int argc, JSValue *argv)
|
||||
@@ -571,13 +571,13 @@ static const JSCFunctionListEntry js_enet_peer_funcs[] = {
|
||||
JSValue js_enet_use(JSContext *ctx)
|
||||
{
|
||||
JS_NewClassID(&enet_host_id);
|
||||
JS_NewClass(JS_GetRuntime(ctx), enet_host_id, &enet_host);
|
||||
JS_NewClass(ctx, enet_host_id, &enet_host);
|
||||
JSValue host_proto = JS_NewObject(ctx);
|
||||
JS_SetPropertyFunctionList(ctx, host_proto, js_enet_host_funcs, countof(js_enet_host_funcs));
|
||||
JS_SetClassProto(ctx, enet_host_id, host_proto);
|
||||
|
||||
JS_NewClassID(&enet_peer_class_id);
|
||||
JS_NewClass(JS_GetRuntime(ctx), enet_peer_class_id, &enet_peer_class);
|
||||
JS_NewClass(ctx, enet_peer_class_id, &enet_peer_class);
|
||||
JSValue peer_proto = JS_NewObject(ctx);
|
||||
JS_SetPropertyFunctionList(ctx, peer_proto, js_enet_peer_funcs, countof(js_enet_peer_funcs));
|
||||
JS_SetClassProto(ctx, enet_peer_class_id, peer_proto);
|
||||
|
||||
@@ -132,7 +132,7 @@ JSC_CCALL(socket_getaddrinfo,
|
||||
// Store the addrinfo pointer as an internal property
|
||||
// We'll need to handle this differently since we can't wrap it
|
||||
// For now, we'll skip storing the raw addrinfo
|
||||
JS_SetPropertyUint32(js, ret, idx++, info);
|
||||
JS_SetPropertyNumber(js, ret, idx++, info);
|
||||
}
|
||||
|
||||
freeaddrinfo(res);
|
||||
|
||||
31
num_torture.cm
Normal file
31
num_torture.cm
Normal file
@@ -0,0 +1,31 @@
|
||||
// num_torture.cm — integer math torture test
|
||||
// Pure integer arithmetic so it stays on the fast int path.
|
||||
// Returns the final checksum so the caller can verify correctness.
|
||||
|
||||
var n = 5000000
|
||||
var sum = 0
|
||||
var i = 0
|
||||
var a = 0
|
||||
var b = 0
|
||||
|
||||
while (i < n) {
|
||||
a = (i * 7 + 13) % 10007
|
||||
b = (a * a) % 10007
|
||||
sum = (sum + b) % 1000000007
|
||||
i = i + 1
|
||||
}
|
||||
|
||||
return function(n) {
|
||||
var i = 0
|
||||
var a = 0
|
||||
var b = 0
|
||||
var sum = 0
|
||||
while (i < n) {
|
||||
a = (i * 7 + 13) % 10007
|
||||
b = (a * a) % 10007
|
||||
sum = (sum + b) % 1000000007
|
||||
i = i + 1
|
||||
}
|
||||
|
||||
return sum
|
||||
}
|
||||
118
package.cm
118
package.cm
@@ -51,7 +51,7 @@ package.load_config = function(name)
|
||||
return config_cache[config_path]
|
||||
|
||||
if (!fd.is_file(config_path)) {
|
||||
throw Error(`${config_path} does not exist`)
|
||||
print(`${config_path} does not exist`); disrupt
|
||||
}
|
||||
|
||||
var content = text(fd.slurp(config_path))
|
||||
@@ -101,11 +101,12 @@ package.alias_to_package = function(name, alias)
|
||||
}
|
||||
|
||||
// alias is optional
|
||||
package.add_dependency = function(name, locator, alias = locator)
|
||||
package.add_dependency = function(name, locator, alias)
|
||||
{
|
||||
var _alias = alias == null ? locator : alias
|
||||
var config = package.load_config(name)
|
||||
if (!config.dependencies) config.dependencies = {}
|
||||
config.dependencies[alias] = locator
|
||||
config.dependencies[_alias] = locator
|
||||
package.save_config(name, config)
|
||||
}
|
||||
|
||||
@@ -115,10 +116,11 @@ package.remove_dependency = function(name, locator)
|
||||
var config = package.load_config(name)
|
||||
if (!config.dependencies) return
|
||||
|
||||
var alias = null
|
||||
if (config.dependencies[locator])
|
||||
delete config.dependencies[locator]
|
||||
else {
|
||||
var alias = package.find_alias(name, locator)
|
||||
alias = package.find_alias(name, locator)
|
||||
if (alias)
|
||||
delete config.dependencies[alias]
|
||||
}
|
||||
@@ -133,8 +135,9 @@ package.find_package_dir = function(file)
|
||||
if (fd.is_file(dir))
|
||||
dir = fd.dirname(dir)
|
||||
|
||||
var toml_path = null
|
||||
while (dir && length(dir) > 0) {
|
||||
var toml_path = dir + '/cell.toml'
|
||||
toml_path = dir + '/cell.toml'
|
||||
if (fd.is_file(toml_path)) {
|
||||
return dir
|
||||
}
|
||||
@@ -158,21 +161,23 @@ package.split_alias = function(name, path)
|
||||
var parts = array(path, '/')
|
||||
var first_part = parts[0]
|
||||
|
||||
try {
|
||||
var _split = function() {
|
||||
var config = package.load_config(name)
|
||||
if (!config) return null
|
||||
|
||||
var deps = config.dependencies
|
||||
var dep_locator = null
|
||||
var remaining_path = null
|
||||
if (deps && deps[first_part]) {
|
||||
var dep_locator = deps[first_part]
|
||||
var remaining_path = text(array(parts, 1), '/')
|
||||
dep_locator = deps[first_part]
|
||||
remaining_path = text(array(parts, 1), '/')
|
||||
return { package: dep_locator, path: remaining_path }
|
||||
}
|
||||
} catch (e) {
|
||||
// Config doesn't exist or couldn't be loaded
|
||||
return null
|
||||
} disruption {
|
||||
return null
|
||||
}
|
||||
|
||||
return null
|
||||
return _split()
|
||||
}
|
||||
|
||||
package.gather_dependencies = function(name)
|
||||
@@ -208,18 +213,23 @@ package.list_files = function(pkg) {
|
||||
var walk = function(current_dir, current_prefix) {
|
||||
var list = fd.readdir(current_dir)
|
||||
if (!list) return
|
||||
|
||||
for (var i = 0; i < length(list); i++) {
|
||||
var item = list[i]
|
||||
|
||||
var i = 0
|
||||
var item = null
|
||||
var full_path = null
|
||||
var rel_path = null
|
||||
var st = null
|
||||
for (i = 0; i < length(list); i++) {
|
||||
item = list[i]
|
||||
if (item == '.' || item == '..') continue
|
||||
if (starts_with(item, '.')) continue
|
||||
|
||||
if (starts_with(item, '.')) continue
|
||||
|
||||
// Skip build directories in root
|
||||
|
||||
var full_path = current_dir + "/" + item
|
||||
var rel_path = current_prefix ? current_prefix + "/" + item : item
|
||||
|
||||
var st = fd.stat(full_path)
|
||||
full_path = current_dir + "/" + item
|
||||
rel_path = current_prefix ? current_prefix + "/" + item : item
|
||||
|
||||
st = fd.stat(full_path)
|
||||
if (st.isDirectory) {
|
||||
walk(full_path, rel_path)
|
||||
} else {
|
||||
@@ -237,7 +247,8 @@ package.list_files = function(pkg) {
|
||||
package.list_modules = function(name) {
|
||||
var files = package.list_files(name)
|
||||
var modules = []
|
||||
for (var i = 0; i < length(files); i++) {
|
||||
var i = 0
|
||||
for (i = 0; i < length(files); i++) {
|
||||
if (ends_with(files[i], '.cm')) {
|
||||
push(modules, text(files[i], 0, -3))
|
||||
}
|
||||
@@ -248,7 +259,8 @@ package.list_modules = function(name) {
|
||||
package.list_programs = function(name) {
|
||||
var files = package.list_files(name)
|
||||
var programs = []
|
||||
for (var i = 0; i < length(files); i++) {
|
||||
var i = 0
|
||||
for (i = 0; i < length(files); i++) {
|
||||
if (ends_with(files[i], '.ce')) {
|
||||
push(programs, text(files[i], 0, -3))
|
||||
}
|
||||
@@ -265,14 +277,16 @@ package.get_flags = function(name, flag_type, target) {
|
||||
var flags = []
|
||||
|
||||
// Base flags
|
||||
var base = null
|
||||
var target_flags = null
|
||||
if (config.compilation && config.compilation[flag_type]) {
|
||||
var base = config.compilation[flag_type]
|
||||
base = config.compilation[flag_type]
|
||||
flags = array(flags, filter(array(base, /\s+/), function(f) { return length(f) > 0 }))
|
||||
}
|
||||
|
||||
|
||||
// Target-specific flags
|
||||
if (target && config.compilation && config.compilation[target] && config.compilation[target][flag_type]) {
|
||||
var target_flags = config.compilation[target][flag_type]
|
||||
target_flags = config.compilation[target][flag_type]
|
||||
flags = array(flags, filter(array(target_flags, /\s+/), function(f) { return length(f) > 0 }))
|
||||
}
|
||||
|
||||
@@ -290,23 +304,36 @@ package.get_c_files = function(name, target, exclude_main) {
|
||||
// Group files by their base name (without target suffix)
|
||||
var groups = {} // base_key -> { generic: file, variants: { target: file } }
|
||||
|
||||
for (var i = 0; i < length(files); i++) {
|
||||
var file = files[i]
|
||||
var i = 0
|
||||
var file = null
|
||||
var ext = null
|
||||
var base = null
|
||||
var name_part = null
|
||||
var dir_part = null
|
||||
var dir = null
|
||||
var is_variant = null
|
||||
var variant_target = null
|
||||
var generic_name = null
|
||||
var t = 0
|
||||
var suffix = null
|
||||
var group_key = null
|
||||
for (i = 0; i < length(files); i++) {
|
||||
file = files[i]
|
||||
if (!ends_with(file, '.c') && !ends_with(file, '.cpp')) continue
|
||||
|
||||
var ext = ends_with(file, '.cpp') ? '.cpp' : '.c'
|
||||
var base = text(file, 0, -length(ext))
|
||||
var name_part = fd.basename(base)
|
||||
var dir_part = fd.dirname(base)
|
||||
var dir = (dir_part && dir_part != '.') ? dir_part + '/' : ''
|
||||
|
||||
|
||||
ext = ends_with(file, '.cpp') ? '.cpp' : '.c'
|
||||
base = text(file, 0, -length(ext))
|
||||
name_part = fd.basename(base)
|
||||
dir_part = fd.dirname(base)
|
||||
dir = (dir_part && dir_part != '.') ? dir_part + '/' : ''
|
||||
|
||||
// Check for target suffix
|
||||
var is_variant = false
|
||||
var variant_target = null
|
||||
var generic_name = name_part
|
||||
|
||||
for (var t = 0; t < length(known_targets); t++) {
|
||||
var suffix = '_' + known_targets[t]
|
||||
is_variant = false
|
||||
variant_target = null
|
||||
generic_name = name_part
|
||||
|
||||
for (t = 0; t < length(known_targets); t++) {
|
||||
suffix = '_' + known_targets[t]
|
||||
if (ends_with(name_part, suffix)) {
|
||||
is_variant = true
|
||||
variant_target = known_targets[t]
|
||||
@@ -315,7 +342,7 @@ package.get_c_files = function(name, target, exclude_main) {
|
||||
}
|
||||
}
|
||||
|
||||
var group_key = dir + generic_name + ext
|
||||
group_key = dir + generic_name + ext
|
||||
if (!groups[group_key]) {
|
||||
groups[group_key] = { generic: null, variants: {} }
|
||||
}
|
||||
@@ -332,18 +359,19 @@ package.get_c_files = function(name, target, exclude_main) {
|
||||
arrfor(array(groups), function(key) {
|
||||
var group = groups[key]
|
||||
var selected = null
|
||||
|
||||
var basename = null
|
||||
|
||||
// Prefer target-specific variant if available
|
||||
if (target && group.variants[target]) {
|
||||
selected = group.variants[target]
|
||||
} else if (group.generic) {
|
||||
selected = group.generic
|
||||
}
|
||||
|
||||
|
||||
if (selected) {
|
||||
// Skip main.c if requested
|
||||
if (exclude_main) {
|
||||
var basename = fd.basename(selected)
|
||||
basename = fd.basename(selected)
|
||||
if (basename == 'main.c' || starts_with(basename, 'main_')) return
|
||||
}
|
||||
push(result, selected)
|
||||
|
||||
9
parse.ce
Normal file
9
parse.ce
Normal file
@@ -0,0 +1,9 @@
|
||||
var fd = use("fd")
|
||||
var json = use("json")
|
||||
var tokenize = use("tokenize")
|
||||
var parse = use("parse")
|
||||
var filename = args[0]
|
||||
var src = text(fd.slurp(filename))
|
||||
var result = tokenize(src, filename)
|
||||
var ast = parse(result.tokens, src, filename, tokenize)
|
||||
print(json.encode(ast))
|
||||
BIN
parse.mach
Normal file
BIN
parse.mach
Normal file
Binary file not shown.
@@ -128,7 +128,7 @@ struct listfiles_ctx {
|
||||
|
||||
static void listfiles_cb(const char *path, void *userdata) {
|
||||
struct listfiles_ctx *ctx = (struct listfiles_ctx*)userdata;
|
||||
JS_SetPropertyUint32(ctx->js, ctx->array, ctx->index++, JS_NewString(ctx->js, path));
|
||||
JS_SetPropertyNumber(ctx->js, ctx->array, ctx->index++, JS_NewString(ctx->js, path));
|
||||
}
|
||||
|
||||
JSC_SCALL(file_listfiles,
|
||||
|
||||
@@ -79,7 +79,7 @@ static void json_decode_array_value(json_decoder *decoder, int pos, json_value v
|
||||
case kJSONString: jsval = JS_NewString(ctx->js, value.data.stringval); break;
|
||||
default: jsval = JS_NULL; break;
|
||||
}
|
||||
JS_SetPropertyUint32(ctx->js, container, pos, jsval);
|
||||
JS_SetPropertyNumber(ctx->js, container, pos, jsval);
|
||||
}
|
||||
|
||||
// --- JSON Encoder Context ---
|
||||
@@ -128,7 +128,7 @@ static void encode_js_array(json_encoder *enc, JSContext *js, JSValue arr) {
|
||||
JS_FreeValue(js, lenVal);
|
||||
for (int i = 0; i < len; i++) {
|
||||
enc->addArrayMember(enc);
|
||||
JSValue val = JS_GetPropertyUint32(js, arr, i);
|
||||
JSValue val = JS_GetPropertyNumber(js, arr, i);
|
||||
encode_js_value(enc, js, val);
|
||||
JS_FreeValue(js, val);
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ static void boards_list_cb(PDBoardsList *boards, const char *errorMessage) {
|
||||
boards->boards[i].boardID ? JS_NewString(g_scoreboard_js, boards->boards[i].boardID) : JS_NULL);
|
||||
JS_SetPropertyStr(g_scoreboard_js, board, "name",
|
||||
boards->boards[i].name ? JS_NewString(g_scoreboard_js, boards->boards[i].name) : JS_NULL);
|
||||
JS_SetPropertyUint32(g_scoreboard_js, arr, i, board);
|
||||
JS_SetPropertyNumber(g_scoreboard_js, arr, i, board);
|
||||
}
|
||||
args[0] = arr;
|
||||
} else {
|
||||
@@ -83,7 +83,7 @@ static void scores_cb(PDScoresList *scores, const char *errorMessage) {
|
||||
JS_SetPropertyStr(g_scoreboard_js, obj, "limit", JS_NewInt32(g_scoreboard_js, scores->limit));
|
||||
JSValue arr = JS_NewArray(g_scoreboard_js);
|
||||
for (unsigned int i = 0; i < scores->count; i++) {
|
||||
JS_SetPropertyUint32(g_scoreboard_js, arr, i, score_to_js(g_scoreboard_js, &scores->scores[i]));
|
||||
JS_SetPropertyNumber(g_scoreboard_js, arr, i, score_to_js(g_scoreboard_js, &scores->scores[i]));
|
||||
}
|
||||
JS_SetPropertyStr(g_scoreboard_js, obj, "scores", arr);
|
||||
args[0] = obj;
|
||||
|
||||
206
pronto.cm
206
pronto.cm
@@ -4,9 +4,9 @@
|
||||
// Time is in seconds.
|
||||
|
||||
function make_reason(factory, excuse, evidence) {
|
||||
def reason = Error(`pronto.${factory}${excuse ? ': ' + excuse : ''}`)
|
||||
reason.evidence = evidence
|
||||
return reason
|
||||
var msg = 'pronto.' + factory
|
||||
if (excuse) msg = msg + ': ' + excuse
|
||||
return { message: msg, evidence: evidence }
|
||||
}
|
||||
|
||||
function is_requestor(fn) {
|
||||
@@ -14,21 +14,27 @@ function is_requestor(fn) {
|
||||
}
|
||||
|
||||
function check_requestors(list, factory) {
|
||||
if (!is_array(list) || some(list, r => !is_requestor(r)))
|
||||
throw make_reason(factory, 'Bad requestor array.', list)
|
||||
if (!is_array(list) || some(list, r => !is_requestor(r))) {
|
||||
print(make_reason(factory, 'Bad requestor array.', list).message + '\n')
|
||||
disrupt
|
||||
}
|
||||
}
|
||||
|
||||
function check_callback(cb, factory) {
|
||||
if (!is_function(cb) || length(cb) != 2)
|
||||
throw make_reason(factory, 'Not a callback.', cb)
|
||||
if (!is_function(cb) || length(cb) != 2) {
|
||||
print(make_reason(factory, 'Not a callback.', cb).message + '\n')
|
||||
disrupt
|
||||
}
|
||||
}
|
||||
|
||||
// fallback(requestor_array)
|
||||
// Tries each requestor in order until one succeeds.
|
||||
function fallback(requestor_array) {
|
||||
def factory = 'fallback'
|
||||
if (!is_array(requestor_array) || length(requestor_array) == 0)
|
||||
throw make_reason(factory, 'Empty requestor array.')
|
||||
if (!is_array(requestor_array) || length(requestor_array) == 0) {
|
||||
print(make_reason(factory, 'Empty requestor array.').message + '\n')
|
||||
disrupt
|
||||
}
|
||||
check_requestors(requestor_array, factory)
|
||||
|
||||
return function fallback_requestor(callback, value) {
|
||||
@@ -38,9 +44,11 @@ function fallback(requestor_array) {
|
||||
var cancelled = false
|
||||
|
||||
function cancel(reason) {
|
||||
var _c = null
|
||||
cancelled = true
|
||||
if (current_cancel) {
|
||||
try { current_cancel(reason) } catch (_) {}
|
||||
_c = function() { current_cancel(reason) } disruption {}
|
||||
_c()
|
||||
current_cancel = null
|
||||
}
|
||||
}
|
||||
@@ -53,9 +61,9 @@ function fallback(requestor_array) {
|
||||
}
|
||||
|
||||
def requestor = requestor_array[index]
|
||||
index += 1
|
||||
index = index + 1
|
||||
|
||||
try {
|
||||
var _run = function() {
|
||||
current_cancel = requestor(function(val, reason) {
|
||||
if (cancelled) return
|
||||
current_cancel = null
|
||||
@@ -65,9 +73,10 @@ function fallback(requestor_array) {
|
||||
try_next()
|
||||
}
|
||||
}, value)
|
||||
} catch (ex) {
|
||||
} disruption {
|
||||
try_next()
|
||||
}
|
||||
_run()
|
||||
}
|
||||
|
||||
try_next()
|
||||
@@ -79,25 +88,32 @@ function fallback(requestor_array) {
|
||||
// Runs requestors in parallel, collecting all results.
|
||||
function parallel(requestor_array, throttle, need) {
|
||||
def factory = 'parallel'
|
||||
if (!is_array(requestor_array))
|
||||
throw make_reason(factory, 'Not an array.', requestor_array)
|
||||
if (!is_array(requestor_array)) {
|
||||
print(make_reason(factory, 'Not an array.', requestor_array).message + '\n')
|
||||
disrupt
|
||||
}
|
||||
check_requestors(requestor_array, factory)
|
||||
|
||||
def length = length(requestor_array)
|
||||
if (length == 0)
|
||||
def len = length(requestor_array)
|
||||
if (len == 0)
|
||||
return function(callback, value) { callback([]) }
|
||||
|
||||
if (need == null) need = length
|
||||
if (!is_number(need) || need < 0 || need > length)
|
||||
throw make_reason(factory, 'Bad need.', need)
|
||||
var _need = need
|
||||
if (_need == null) _need = len
|
||||
if (!is_number(_need) || _need < 0 || _need > len) {
|
||||
print(make_reason(factory, 'Bad need.', _need).message + '\n')
|
||||
disrupt
|
||||
}
|
||||
|
||||
if (throttle != null && (!is_number(throttle) || throttle < 1))
|
||||
throw make_reason(factory, 'Bad throttle.', throttle)
|
||||
if (throttle != null && (!is_number(throttle) || throttle < 1)) {
|
||||
print(make_reason(factory, 'Bad throttle.', throttle).message + '\n')
|
||||
disrupt
|
||||
}
|
||||
|
||||
return function parallel_requestor(callback, value) {
|
||||
check_callback(callback, factory)
|
||||
def results = array(length)
|
||||
def cancel_list = array(length)
|
||||
def results = array(len)
|
||||
def cancel_list = array(len)
|
||||
var next_index = 0
|
||||
var successes = 0
|
||||
var failures = 0
|
||||
@@ -107,33 +123,34 @@ function parallel(requestor_array, throttle, need) {
|
||||
if (finished) return
|
||||
finished = true
|
||||
arrfor(cancel_list, c => {
|
||||
try { if (is_function(c)) c(reason) } catch (_) {}
|
||||
var _c = function() { if (is_function(c)) c(reason) } disruption {}
|
||||
_c()
|
||||
})
|
||||
}
|
||||
|
||||
function start_one() {
|
||||
if (finished || next_index >= length) return
|
||||
if (finished || next_index >= len) return
|
||||
def idx = next_index
|
||||
next_index += 1
|
||||
next_index = next_index + 1
|
||||
def requestor = requestor_array[idx]
|
||||
|
||||
try {
|
||||
var _run = function() {
|
||||
cancel_list[idx] = requestor(function(val, reason) {
|
||||
if (finished) return
|
||||
cancel_list[idx] = null
|
||||
|
||||
if (val != null) {
|
||||
results[idx] = val
|
||||
successes += 1
|
||||
if (successes >= need) {
|
||||
successes = successes + 1
|
||||
if (successes >= _need) {
|
||||
finished = true
|
||||
cancel(make_reason(factory, 'Finished.'))
|
||||
callback(results)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
failures += 1
|
||||
if (failures > length - need) {
|
||||
failures = failures + 1
|
||||
if (failures > len - _need) {
|
||||
cancel(reason)
|
||||
callback(null, reason || make_reason(factory, 'Too many failures.'))
|
||||
return
|
||||
@@ -142,20 +159,21 @@ function parallel(requestor_array, throttle, need) {
|
||||
|
||||
start_one()
|
||||
}, value)
|
||||
} catch (ex) {
|
||||
failures += 1
|
||||
if (failures > length - need) {
|
||||
cancel(ex)
|
||||
callback(null, ex)
|
||||
} disruption {
|
||||
failures = failures + 1
|
||||
if (failures > len - _need) {
|
||||
cancel(make_reason(factory, 'Requestor threw.'))
|
||||
callback(null, make_reason(factory, 'Requestor threw.'))
|
||||
return
|
||||
}
|
||||
start_one()
|
||||
}
|
||||
_run()
|
||||
}
|
||||
|
||||
|
||||
def concurrent = throttle ? min(throttle, length) : length
|
||||
for (var i = 0; i < concurrent; i++) start_one()
|
||||
def concurrent = throttle ? min(throttle, len) : len
|
||||
var i = 0
|
||||
while (i < concurrent) { start_one(); i = i + 1 }
|
||||
|
||||
return cancel
|
||||
}
|
||||
@@ -165,22 +183,29 @@ function parallel(requestor_array, throttle, need) {
|
||||
// Runs requestors in parallel, returns first success(es).
|
||||
function race(requestor_array, throttle, need) {
|
||||
def factory = 'race'
|
||||
if (!is_array(requestor_array) || length(requestor_array) == 0)
|
||||
throw make_reason(factory, 'Empty requestor array.')
|
||||
if (!is_array(requestor_array) || length(requestor_array) == 0) {
|
||||
print(make_reason(factory, 'Empty requestor array.').message + '\n')
|
||||
disrupt
|
||||
}
|
||||
check_requestors(requestor_array, factory)
|
||||
|
||||
def length = length(requestor_array)
|
||||
if (need == null) need = 1
|
||||
if (!is_number(need) || need < 1 || need > length)
|
||||
throw make_reason(factory, 'Bad need.', need)
|
||||
def len = length(requestor_array)
|
||||
var _need = need
|
||||
if (_need == null) _need = 1
|
||||
if (!is_number(_need) || _need < 1 || _need > len) {
|
||||
print(make_reason(factory, 'Bad need.', _need).message + '\n')
|
||||
disrupt
|
||||
}
|
||||
|
||||
if (throttle != null && (!is_number(throttle) || throttle < 1))
|
||||
throw make_reason(factory, 'Bad throttle.', throttle)
|
||||
if (throttle != null && (!is_number(throttle) || throttle < 1)) {
|
||||
print(make_reason(factory, 'Bad throttle.', throttle).message + '\n')
|
||||
disrupt
|
||||
}
|
||||
|
||||
return function race_requestor(callback, value) {
|
||||
check_callback(callback, factory)
|
||||
def results = array(length)
|
||||
def cancel_list = array(length)
|
||||
def results = array(len)
|
||||
def cancel_list = array(len)
|
||||
var next_index = 0
|
||||
var successes = 0
|
||||
var failures = 0
|
||||
@@ -190,27 +215,28 @@ function race(requestor_array, throttle, need) {
|
||||
if (finished) return
|
||||
finished = true
|
||||
arrfor(cancel_list, c => {
|
||||
try { if (is_function(c)) c(reason) } catch (_) {}
|
||||
var _c = function() { if (is_function(c)) c(reason) } disruption {}
|
||||
_c()
|
||||
})
|
||||
}
|
||||
|
||||
function start_one() {
|
||||
if (finished || next_index >= length) return
|
||||
if (finished || next_index >= len) return
|
||||
def idx = next_index
|
||||
next_index += 1
|
||||
next_index = next_index + 1
|
||||
def requestor = requestor_array[idx]
|
||||
|
||||
try {
|
||||
var _run = function() {
|
||||
cancel_list[idx] = requestor(function(val, reason) {
|
||||
if (finished) return
|
||||
cancel_list[idx] = null
|
||||
|
||||
if (val != null) {
|
||||
results[idx] = val
|
||||
successes += 1
|
||||
if (successes >= need) {
|
||||
successes = successes + 1
|
||||
if (successes >= _need) {
|
||||
cancel(make_reason(factory, 'Winner.'))
|
||||
if (need == 1) {
|
||||
if (_need == 1) {
|
||||
callback(val)
|
||||
} else {
|
||||
callback(results)
|
||||
@@ -218,8 +244,8 @@ function race(requestor_array, throttle, need) {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
failures += 1
|
||||
if (failures > length - need) {
|
||||
failures = failures + 1
|
||||
if (failures > len - _need) {
|
||||
cancel(reason)
|
||||
callback(null, reason || make_reason(factory, 'All failed.'))
|
||||
return
|
||||
@@ -228,19 +254,21 @@ function race(requestor_array, throttle, need) {
|
||||
|
||||
start_one()
|
||||
}, value)
|
||||
} catch (ex) {
|
||||
failures += 1
|
||||
if (failures > length - need) {
|
||||
cancel(ex)
|
||||
callback(null, ex)
|
||||
} disruption {
|
||||
failures = failures + 1
|
||||
if (failures > len - _need) {
|
||||
cancel(make_reason(factory, 'Requestor threw.'))
|
||||
callback(null, make_reason(factory, 'Requestor threw.'))
|
||||
return
|
||||
}
|
||||
start_one()
|
||||
}
|
||||
_run()
|
||||
}
|
||||
|
||||
def concurrent = throttle ? min(throttle, length) : length
|
||||
for (var i = 0; i < concurrent; i++) start_one()
|
||||
def concurrent = throttle ? min(throttle, len) : len
|
||||
var i = 0
|
||||
while (i < concurrent) { start_one(); i = i + 1 }
|
||||
|
||||
return cancel
|
||||
}
|
||||
@@ -250,8 +278,10 @@ function race(requestor_array, throttle, need) {
|
||||
// Runs requestors one at a time, passing result to next.
|
||||
function sequence(requestor_array) {
|
||||
def factory = 'sequence'
|
||||
if (!is_array(requestor_array))
|
||||
throw make_reason(factory, 'Not an array.', requestor_array)
|
||||
if (!is_array(requestor_array)) {
|
||||
print(make_reason(factory, 'Not an array.', requestor_array).message + '\n')
|
||||
disrupt
|
||||
}
|
||||
check_requestors(requestor_array, factory)
|
||||
|
||||
if (length(requestor_array) == 0)
|
||||
@@ -264,9 +294,11 @@ function sequence(requestor_array) {
|
||||
var cancelled = false
|
||||
|
||||
function cancel(reason) {
|
||||
var _c = null
|
||||
cancelled = true
|
||||
if (current_cancel) {
|
||||
try { current_cancel(reason) } catch (_) {}
|
||||
_c = function() { current_cancel(reason) } disruption {}
|
||||
_c()
|
||||
current_cancel = null
|
||||
}
|
||||
}
|
||||
@@ -279,9 +311,9 @@ function sequence(requestor_array) {
|
||||
}
|
||||
|
||||
def requestor = requestor_array[index]
|
||||
index += 1
|
||||
index = index + 1
|
||||
|
||||
try {
|
||||
var _run = function() {
|
||||
current_cancel = requestor(function(result, reason) {
|
||||
if (cancelled) return
|
||||
current_cancel = null
|
||||
@@ -291,9 +323,10 @@ function sequence(requestor_array) {
|
||||
run_next(result)
|
||||
}
|
||||
}, val)
|
||||
} catch (ex) {
|
||||
callback(null, ex)
|
||||
} disruption {
|
||||
callback(null, make_reason(factory, 'Requestor threw.'))
|
||||
}
|
||||
_run()
|
||||
}
|
||||
|
||||
run_next(value)
|
||||
@@ -305,26 +338,29 @@ function sequence(requestor_array) {
|
||||
// Converts a unary function into a requestor.
|
||||
function requestorize(unary) {
|
||||
def factory = 'requestorize'
|
||||
if (!is_function(unary))
|
||||
throw make_reason(factory, 'Not a function.', unary)
|
||||
if (!is_function(unary)) {
|
||||
print(make_reason(factory, 'Not a function.', unary).message + '\n')
|
||||
disrupt
|
||||
}
|
||||
|
||||
return function requestorized(callback, value) {
|
||||
check_callback(callback, factory)
|
||||
try {
|
||||
var _run = function() {
|
||||
def result = unary(value)
|
||||
callback(result == null ? true : result)
|
||||
} catch (ex) {
|
||||
callback(null, ex)
|
||||
} disruption {
|
||||
callback(null, make_reason(factory, 'Function threw.'))
|
||||
}
|
||||
_run()
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
fallback,
|
||||
parallel,
|
||||
race,
|
||||
sequence,
|
||||
requestorize,
|
||||
is_requestor,
|
||||
check_callback
|
||||
fallback: fallback,
|
||||
parallel: parallel,
|
||||
race: race,
|
||||
sequence: sequence,
|
||||
requestorize: requestorize,
|
||||
is_requestor: is_requestor,
|
||||
check_callback: check_callback
|
||||
}
|
||||
|
||||
18
qbe.ce
Normal file
18
qbe.ce
Normal file
@@ -0,0 +1,18 @@
|
||||
var fd = use("fd")
|
||||
var json = use("json")
|
||||
var tokenize = use("tokenize")
|
||||
var parse = use("parse")
|
||||
var fold = use("fold")
|
||||
var mcode = use("mcode")
|
||||
var streamline = use("streamline")
|
||||
var qbe_macros = use("qbe")
|
||||
var qbe_emit = use("qbe_emit")
|
||||
var filename = args[0]
|
||||
var src = text(fd.slurp(filename))
|
||||
var result = tokenize(src, filename)
|
||||
var ast = parse(result.tokens, src, filename, tokenize)
|
||||
var folded = fold(ast)
|
||||
var compiled = mcode(folded)
|
||||
var optimized = streamline(compiled)
|
||||
var il = qbe_emit(optimized, qbe_macros)
|
||||
print(il)
|
||||
757
qbe_emit.cm
Normal file
757
qbe_emit.cm
Normal file
@@ -0,0 +1,757 @@
|
||||
// qbe_emit.cm — mcode IR → QBE IL compiler
|
||||
// Takes mcode IR (from mcode.cm) and uses qbe.cm macros to produce
|
||||
// a complete QBE IL program ready for the qbe compiler.
|
||||
// qbe module is passed via env as 'qbe'
|
||||
|
||||
var qbe_emit = function(ir, qbe) {
|
||||
var out = []
|
||||
var data_out = []
|
||||
var str_table = {}
|
||||
var str_id = 0
|
||||
var uid = 0
|
||||
|
||||
// ============================================================
|
||||
// Output helpers
|
||||
// ============================================================
|
||||
|
||||
var emit = function(s) {
|
||||
push(out, s)
|
||||
}
|
||||
|
||||
var fresh = function() {
|
||||
uid = uid + 1
|
||||
return "u" + text(uid)
|
||||
}
|
||||
|
||||
var s = function(n) {
|
||||
return "%s" + text(n)
|
||||
}
|
||||
|
||||
var sanitize = function(lbl) {
|
||||
var r = replace(lbl, ".", "_")
|
||||
r = replace(r, "-", "_")
|
||||
r = replace(r, " ", "_")
|
||||
r = replace(r, "/", "_")
|
||||
r = replace(r, "<", "")
|
||||
r = replace(r, ">", "")
|
||||
r = replace(r, "(", "")
|
||||
r = replace(r, ")", "")
|
||||
return r
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// String interning — emit data section entries
|
||||
// ============================================================
|
||||
|
||||
var intern_str = function(val) {
|
||||
if (str_table[val] != null) return str_table[val]
|
||||
var label = "$d_str_" + text(str_id)
|
||||
str_id = str_id + 1
|
||||
var escaped = replace(val, "\\", "\\\\")
|
||||
escaped = replace(escaped, "\"", "\\\"")
|
||||
var line = "data " + label + ' = ' + '{ b "' + escaped + '", b 0 }'
|
||||
push(data_out, line)
|
||||
str_table[val] = label
|
||||
return label
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// Extract property name from mcode operand
|
||||
// ============================================================
|
||||
|
||||
var prop_name = function(a) {
|
||||
if (is_text(a)) return a
|
||||
if (is_object(a)) {
|
||||
if (a.name != null) return a.name
|
||||
if (a.value != null) return a.value
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// Compile one function's instructions
|
||||
// ============================================================
|
||||
|
||||
var compile_fn = function(fn, fn_idx, is_main) {
|
||||
var instrs = fn.instructions
|
||||
var nr_slots = fn.nr_slots
|
||||
var nr_args = fn.nr_args
|
||||
var name = is_main ? "cell_main" : "cell_fn_" + text(fn_idx)
|
||||
name = sanitize(name)
|
||||
var i = 0
|
||||
var instr = null
|
||||
var op = null
|
||||
var a1 = null
|
||||
var a2 = null
|
||||
var a3 = null
|
||||
var a4 = null
|
||||
var p = null
|
||||
var pn = null
|
||||
var sl = null
|
||||
var fop_id = 0
|
||||
var nr_elems = 0
|
||||
var ei = 0
|
||||
var elem_slot = 0
|
||||
|
||||
// Function signature: (ctx, frame_ptr) → JSValue
|
||||
emit(`export function l $${name}(l %ctx, l %fp) {`)
|
||||
emit("@entry")
|
||||
|
||||
// Load all slots from frame into SSA variables
|
||||
// Each slot is a JSValue (8 bytes) at fp + slot*8
|
||||
var off = 0
|
||||
i = 0
|
||||
while (i < nr_slots) {
|
||||
off = i * 8
|
||||
emit(` %p${text(i)} =l add %fp, ${text(off)}`)
|
||||
emit(` ${s(i)} =l loadl %p${text(i)}`)
|
||||
i = i + 1
|
||||
}
|
||||
|
||||
// Write-back: store SSA var to frame slot so closures see updates
|
||||
var wb = function(slot) {
|
||||
emit(` storel ${s(slot)}, %p${text(slot)}`)
|
||||
}
|
||||
|
||||
// Walk instructions
|
||||
i = 0
|
||||
while (i < length(instrs)) {
|
||||
instr = instrs[i]
|
||||
i = i + 1
|
||||
|
||||
// Labels are plain strings
|
||||
if (is_text(instr)) {
|
||||
emit("@" + sanitize(instr))
|
||||
continue
|
||||
}
|
||||
|
||||
op = instr[0]
|
||||
a1 = instr[1]
|
||||
a2 = instr[2]
|
||||
a3 = instr[3]
|
||||
|
||||
// --- Constants ---
|
||||
|
||||
if (op == "int") {
|
||||
emit(` ${s(a1)} =l copy ${text(a2 * 2)}`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
if (op == "null") {
|
||||
emit(` ${s(a1)} =l copy ${text(qbe.js_null)}`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
if (op == "true") {
|
||||
emit(` ${s(a1)} =l copy ${text(qbe.js_true)}`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
if (op == "false") {
|
||||
emit(` ${s(a1)} =l copy ${text(qbe.js_false)}`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
if (op == "access") {
|
||||
if (is_number(a2)) {
|
||||
if (is_integer(a2)) {
|
||||
emit(` ${s(a1)} =l copy ${text(a2 * 2)}`)
|
||||
} else {
|
||||
emit(` ${s(a1)} =l call $__JS_NewFloat64(l %ctx, d d_${text(a2)})`)
|
||||
}
|
||||
} else if (is_text(a2)) {
|
||||
sl = intern_str(a2)
|
||||
emit(` ${s(a1)} =l call $JS_NewString(l %ctx, l ${sl})`)
|
||||
} else if (is_object(a2)) {
|
||||
if (a2.make == "intrinsic") {
|
||||
sl = intern_str(a2.name)
|
||||
emit(` ${s(a1)} =l call $cell_rt_get_intrinsic(l %ctx, l ${sl})`)
|
||||
} else if (a2.kind == "number") {
|
||||
if (a2.number != null && is_integer(a2.number)) {
|
||||
emit(` ${s(a1)} =l copy ${text(a2.number * 2)}`)
|
||||
} else if (a2.number != null) {
|
||||
emit(` ${s(a1)} =l call $__JS_NewFloat64(l %ctx, d d_${text(a2.number)})`)
|
||||
} else {
|
||||
emit(` ${s(a1)} =l copy ${text(qbe.js_null)}`)
|
||||
}
|
||||
} else if (a2.kind == "text") {
|
||||
sl = intern_str(a2.value)
|
||||
emit(` ${s(a1)} =l call $JS_NewString(l %ctx, l ${sl})`)
|
||||
} else if (a2.kind == "true") {
|
||||
emit(` ${s(a1)} =l copy ${text(qbe.js_true)}`)
|
||||
} else if (a2.kind == "false") {
|
||||
emit(` ${s(a1)} =l copy ${text(qbe.js_false)}`)
|
||||
} else if (a2.kind == "null") {
|
||||
emit(` ${s(a1)} =l copy ${text(qbe.js_null)}`)
|
||||
} else {
|
||||
emit(` ${s(a1)} =l copy ${text(qbe.js_null)}`)
|
||||
}
|
||||
} else {
|
||||
emit(` ${s(a1)} =l copy ${text(qbe.js_null)}`)
|
||||
}
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
|
||||
// --- Movement ---
|
||||
|
||||
if (op == "move") {
|
||||
emit(` ${s(a1)} =l copy ${s(a2)}`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
|
||||
// --- Arithmetic (int path) — use qbe.cm macros ---
|
||||
|
||||
if (op == "add_int") {
|
||||
p = fresh()
|
||||
emit(qbe.add_int(p, "%ctx", s(a2), s(a3)))
|
||||
emit(` ${s(a1)} =l copy %${p}`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
if (op == "sub_int") {
|
||||
p = fresh()
|
||||
emit(qbe.sub_int(p, "%ctx", s(a2), s(a3)))
|
||||
emit(` ${s(a1)} =l copy %${p}`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
if (op == "mul_int") {
|
||||
p = fresh()
|
||||
emit(qbe.mul_int(p, "%ctx", s(a2), s(a3)))
|
||||
emit(` ${s(a1)} =l copy %${p}`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
if (op == "div_int") {
|
||||
p = fresh()
|
||||
emit(qbe.div_int(p, "%ctx", s(a2), s(a3)))
|
||||
emit(` ${s(a1)} =l copy %${p}`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
if (op == "mod_int") {
|
||||
p = fresh()
|
||||
emit(qbe.mod_int(p, "%ctx", s(a2), s(a3)))
|
||||
emit(` ${s(a1)} =l copy %${p}`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
|
||||
// --- Arithmetic (float path) ---
|
||||
|
||||
if (op == "add_float") {
|
||||
p = fresh()
|
||||
emit(qbe.add_float(p, "%ctx", s(a2), s(a3)))
|
||||
emit(` ${s(a1)} =l copy %${p}`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
if (op == "sub_float") {
|
||||
p = fresh()
|
||||
emit(qbe.sub_float(p, "%ctx", s(a2), s(a3)))
|
||||
emit(` ${s(a1)} =l copy %${p}`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
if (op == "mul_float") {
|
||||
p = fresh()
|
||||
emit(qbe.mul_float(p, "%ctx", s(a2), s(a3)))
|
||||
emit(` ${s(a1)} =l copy %${p}`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
if (op == "div_float") {
|
||||
p = fresh()
|
||||
emit(qbe.div_float(p, "%ctx", s(a2), s(a3)))
|
||||
emit(` ${s(a1)} =l copy %${p}`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
if (op == "mod_float") {
|
||||
p = fresh()
|
||||
emit(qbe.mod_float(p, "%ctx", s(a2), s(a3)))
|
||||
emit(` ${s(a1)} =l copy %${p}`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
|
||||
// --- String concat ---
|
||||
|
||||
if (op == "concat") {
|
||||
p = fresh()
|
||||
emit(qbe.concat(p, "%ctx", s(a2), s(a3)))
|
||||
emit(` ${s(a1)} =l copy %${p}`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
|
||||
// --- Type checks — use qbe.cm macros ---
|
||||
|
||||
if (op == "is_int") {
|
||||
p = fresh()
|
||||
emit(qbe.is_int(p, s(a2)))
|
||||
emit(qbe.new_bool(p + ".r", "%" + p))
|
||||
emit(` ${s(a1)} =l copy %${p}.r`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
if (op == "is_text") {
|
||||
p = fresh()
|
||||
emit(qbe.is_imm_text(p, s(a2)))
|
||||
emit(qbe.new_bool(p + ".r", "%" + p))
|
||||
emit(` ${s(a1)} =l copy %${p}.r`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
if (op == "is_num") {
|
||||
p = fresh()
|
||||
emit(qbe.is_number(p, s(a2)))
|
||||
emit(qbe.new_bool(p + ".r", "%" + p))
|
||||
emit(` ${s(a1)} =l copy %${p}.r`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
if (op == "is_bool") {
|
||||
p = fresh()
|
||||
emit(qbe.is_bool(p, s(a2)))
|
||||
emit(qbe.new_bool(p + ".r", "%" + p))
|
||||
emit(` ${s(a1)} =l copy %${p}.r`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
if (op == "is_null") {
|
||||
p = fresh()
|
||||
emit(qbe.is_null(p, s(a2)))
|
||||
emit(qbe.new_bool(p + ".r", "%" + p))
|
||||
emit(` ${s(a1)} =l copy %${p}.r`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
if (op == "is_identical") {
|
||||
p = fresh()
|
||||
emit(qbe.is_identical(p, s(a2), s(a3)))
|
||||
emit(` ${s(a1)} =l copy %${p}`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
|
||||
// --- Comparisons (int path) ---
|
||||
|
||||
if (op == "eq_int") {
|
||||
p = fresh()
|
||||
emit(qbe.eq_int(p, "%ctx", s(a2), s(a3)))
|
||||
emit(` ${s(a1)} =l copy %${p}`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
if (op == "ne_int") {
|
||||
p = fresh()
|
||||
emit(qbe.ne_int(p, "%ctx", s(a2), s(a3)))
|
||||
emit(` ${s(a1)} =l copy %${p}`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
if (op == "lt_int") {
|
||||
p = fresh()
|
||||
emit(qbe.lt_int(p, "%ctx", s(a2), s(a3)))
|
||||
emit(` ${s(a1)} =l copy %${p}`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
if (op == "gt_int") {
|
||||
p = fresh()
|
||||
emit(qbe.gt_int(p, "%ctx", s(a2), s(a3)))
|
||||
emit(` ${s(a1)} =l copy %${p}`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
if (op == "le_int") {
|
||||
p = fresh()
|
||||
emit(qbe.le_int(p, "%ctx", s(a2), s(a3)))
|
||||
emit(` ${s(a1)} =l copy %${p}`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
if (op == "ge_int") {
|
||||
p = fresh()
|
||||
emit(qbe.ge_int(p, "%ctx", s(a2), s(a3)))
|
||||
emit(` ${s(a1)} =l copy %${p}`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
|
||||
// --- Comparisons (float/text/bool) ---
|
||||
|
||||
if (op == "eq_float") {
|
||||
p = fresh()
|
||||
emit(qbe.eq_float(p, "%ctx", s(a2), s(a3)))
|
||||
emit(` ${s(a1)} =l copy %${p}`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
if (op == "ne_float") {
|
||||
p = fresh()
|
||||
emit(qbe.ne_float(p, "%ctx", s(a2), s(a3)))
|
||||
emit(` ${s(a1)} =l copy %${p}`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
if (op == "lt_float" || op == "gt_float" || op == "le_float" || op == "ge_float") {
|
||||
p = fresh()
|
||||
fop_id = 0
|
||||
if (op == "lt_float") fop_id = 2
|
||||
else if (op == "le_float") fop_id = 3
|
||||
else if (op == "gt_float") fop_id = 4
|
||||
else if (op == "ge_float") fop_id = 5
|
||||
emit(qbe.cmp_float != null ? qbe.cmp_float(p, "%ctx", s(a2), s(a3), fop_id) : ` %${p} =l call $qbe_float_cmp(l %ctx, w ${text(fop_id)}, l ${s(a2)}, l ${s(a3)})`)
|
||||
emit(` ${s(a1)} =l copy %${p}`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
if (op == "eq_text") {
|
||||
p = fresh()
|
||||
emit(qbe.eq_text(p, "%ctx", s(a2), s(a3)))
|
||||
emit(` ${s(a1)} =l copy %${p}`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
if (op == "ne_text") {
|
||||
p = fresh()
|
||||
emit(qbe.ne_text(p, "%ctx", s(a2), s(a3)))
|
||||
emit(` ${s(a1)} =l copy %${p}`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
if (op == "lt_text" || op == "gt_text" || op == "le_text" || op == "ge_text") {
|
||||
p = fresh()
|
||||
emit(` ${s(a1)} =l call $cell_rt_${op}(l %ctx, l ${s(a2)}, l ${s(a3)})`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
if (op == "eq_bool") {
|
||||
p = fresh()
|
||||
emit(qbe.eq_bool(p, s(a2), s(a3)))
|
||||
emit(` ${s(a1)} =l copy %${p}`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
if (op == "ne_bool") {
|
||||
p = fresh()
|
||||
emit(qbe.ne_bool(p, s(a2), s(a3)))
|
||||
emit(` ${s(a1)} =l copy %${p}`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
if (op == "eq_tol" || op == "ne_tol") {
|
||||
emit(` ${s(a1)} =l call $cell_rt_${op}(l %ctx, l ${s(a2)}, l ${s(a3)})`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
|
||||
// --- Boolean ops ---
|
||||
|
||||
if (op == "not") {
|
||||
p = fresh()
|
||||
emit(qbe.lnot(p, "%ctx", s(a2)))
|
||||
emit(` ${s(a1)} =l copy %${p}`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
if (op == "and") {
|
||||
emit(` ${s(a1)} =l and ${s(a2)}, ${s(a3)}`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
if (op == "or") {
|
||||
emit(` ${s(a1)} =l or ${s(a2)}, ${s(a3)}`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
|
||||
// --- Bitwise ops — use qbe.cm macros ---
|
||||
|
||||
if (op == "bitnot") {
|
||||
p = fresh()
|
||||
emit(qbe.bnot(p, "%ctx", s(a2)))
|
||||
emit(` ${s(a1)} =l copy %${p}`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
if (op == "bitand") {
|
||||
p = fresh()
|
||||
emit(qbe.band(p, "%ctx", s(a2), s(a3)))
|
||||
emit(` ${s(a1)} =l copy %${p}`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
if (op == "bitor") {
|
||||
p = fresh()
|
||||
emit(qbe.bor(p, "%ctx", s(a2), s(a3)))
|
||||
emit(` ${s(a1)} =l copy %${p}`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
if (op == "bitxor") {
|
||||
p = fresh()
|
||||
emit(qbe.bxor(p, "%ctx", s(a2), s(a3)))
|
||||
emit(` ${s(a1)} =l copy %${p}`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
if (op == "shl") {
|
||||
p = fresh()
|
||||
emit(qbe.shl(p, "%ctx", s(a2), s(a3)))
|
||||
emit(` ${s(a1)} =l copy %${p}`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
if (op == "shr") {
|
||||
p = fresh()
|
||||
emit(qbe.shr(p, "%ctx", s(a2), s(a3)))
|
||||
emit(` ${s(a1)} =l copy %${p}`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
if (op == "ushr") {
|
||||
p = fresh()
|
||||
emit(qbe.ushr(p, "%ctx", s(a2), s(a3)))
|
||||
emit(` ${s(a1)} =l copy %${p}`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
|
||||
// --- Property access — runtime calls ---
|
||||
|
||||
if (op == "load_field") {
|
||||
pn = prop_name(a3)
|
||||
if (pn != null) {
|
||||
sl = intern_str(pn)
|
||||
emit(` ${s(a1)} =l call $cell_rt_load_field(l %ctx, l ${s(a2)}, l ${sl})`)
|
||||
} else {
|
||||
emit(` ${s(a1)} =l call $cell_rt_load_dynamic(l %ctx, l ${s(a2)}, l ${s(a3)})`)
|
||||
}
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
if (op == "load_index") {
|
||||
emit(` ${s(a1)} =l call $cell_rt_load_index(l %ctx, l ${s(a2)}, l ${s(a3)})`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
if (op == "load_dynamic") {
|
||||
emit(` ${s(a1)} =l call $cell_rt_load_dynamic(l %ctx, l ${s(a2)}, l ${s(a3)})`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
if (op == "store_field") {
|
||||
// IR: ["store_field", obj, val, prop] → C: (ctx, val, obj, name)
|
||||
pn = prop_name(a3)
|
||||
if (pn != null) {
|
||||
sl = intern_str(pn)
|
||||
emit(` call $cell_rt_store_field(l %ctx, l ${s(a2)}, l ${s(a1)}, l ${sl})`)
|
||||
} else {
|
||||
emit(` call $cell_rt_store_dynamic(l %ctx, l ${s(a2)}, l ${s(a1)}, l ${s(a3)})`)
|
||||
}
|
||||
continue
|
||||
}
|
||||
if (op == "store_index") {
|
||||
// IR: ["store_index", obj, val, idx] → C: (ctx, val, obj, idx)
|
||||
emit(` call $cell_rt_store_index(l %ctx, l ${s(a2)}, l ${s(a1)}, l ${s(a3)})`)
|
||||
continue
|
||||
}
|
||||
if (op == "store_dynamic") {
|
||||
// IR: ["store_dynamic", obj, val, key] → C: (ctx, val, obj, key)
|
||||
emit(` call $cell_rt_store_dynamic(l %ctx, l ${s(a2)}, l ${s(a1)}, l ${s(a3)})`)
|
||||
continue
|
||||
}
|
||||
|
||||
// --- Closure access ---
|
||||
|
||||
if (op == "get") {
|
||||
emit(` ${s(a1)} =l call $cell_rt_get_closure(l %ctx, l %fp, l ${text(a2)}, l ${text(a3)})`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
if (op == "put") {
|
||||
emit(` call $cell_rt_put_closure(l %ctx, l %fp, l ${s(a1)}, l ${text(a2)}, l ${text(a3)})`)
|
||||
continue
|
||||
}
|
||||
|
||||
// --- Control flow ---
|
||||
|
||||
if (op == "jump") {
|
||||
emit(` jmp @${sanitize(a1)}`)
|
||||
continue
|
||||
}
|
||||
if (op == "jump_true") {
|
||||
p = fresh()
|
||||
emit(` %${p} =w call $JS_ToBool(l %ctx, l ${s(a1)})`)
|
||||
emit(` jnz %${p}, @${sanitize(a2)}, @${p}_f`)
|
||||
emit(`@${p}_f`)
|
||||
continue
|
||||
}
|
||||
if (op == "jump_false") {
|
||||
p = fresh()
|
||||
emit(` %${p} =w call $JS_ToBool(l %ctx, l ${s(a1)})`)
|
||||
emit(` jnz %${p}, @${p}_t, @${sanitize(a2)}`)
|
||||
emit(`@${p}_t`)
|
||||
continue
|
||||
}
|
||||
if (op == "jump_null") {
|
||||
p = fresh()
|
||||
emit(` %${p} =w ceql ${s(a1)}, ${text(qbe.js_null)}`)
|
||||
emit(` jnz %${p}, @${sanitize(a2)}, @${p}_nn`)
|
||||
emit(`@${p}_nn`)
|
||||
continue
|
||||
}
|
||||
if (op == "jump_not_null") {
|
||||
p = fresh()
|
||||
emit(` %${p} =w cnel ${s(a1)}, ${text(qbe.js_null)}`)
|
||||
emit(` jnz %${p}, @${sanitize(a2)}, @${p}_n`)
|
||||
emit(`@${p}_n`)
|
||||
continue
|
||||
}
|
||||
if (op == "wary_true") {
|
||||
p = fresh()
|
||||
emit(` %${p} =w call $JS_ToBool(l %ctx, l ${s(a1)})`)
|
||||
emit(` jnz %${p}, @${sanitize(a2)}, @${p}_f`)
|
||||
emit(`@${p}_f`)
|
||||
continue
|
||||
}
|
||||
if (op == "wary_false") {
|
||||
p = fresh()
|
||||
emit(` %${p} =w call $JS_ToBool(l %ctx, l ${s(a1)})`)
|
||||
emit(` jnz %${p}, @${p}_t, @${sanitize(a2)}`)
|
||||
emit(`@${p}_t`)
|
||||
continue
|
||||
}
|
||||
|
||||
// --- Function calls ---
|
||||
|
||||
if (op == "frame") {
|
||||
emit(` ${s(a1)} =l call $cell_rt_frame(l %ctx, l ${s(a2)}, l ${text(a3)})`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
if (op == "setarg") {
|
||||
emit(` call $cell_rt_setarg(l ${s(a1)}, l ${text(a2)}, l ${s(a3)})`)
|
||||
continue
|
||||
}
|
||||
if (op == "invoke") {
|
||||
emit(` ${s(a2)} =l call $cell_rt_invoke(l %ctx, l ${s(a1)})`)
|
||||
wb(a2)
|
||||
continue
|
||||
}
|
||||
if (op == "goframe") {
|
||||
emit(` ${s(a1)} =l call $cell_rt_goframe(l %ctx, l ${s(a2)}, l ${text(a3)})`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
if (op == "goinvoke") {
|
||||
emit(` call $cell_rt_goinvoke(l %ctx, l ${s(a1)})`)
|
||||
continue
|
||||
}
|
||||
|
||||
// --- Function object creation ---
|
||||
|
||||
if (op == "function") {
|
||||
emit(` ${s(a1)} =l call $cell_rt_make_function(l %ctx, l ${text(a2)}, l %fp)`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
|
||||
// --- Record/Array creation ---
|
||||
|
||||
if (op == "record") {
|
||||
emit(` ${s(a1)} =l call $JS_NewObject(l %ctx)`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
if (op == "array") {
|
||||
nr_elems = a2 != null ? a2 : 0
|
||||
emit(` ${s(a1)} =l call $JS_NewArray(l %ctx)`)
|
||||
ei = 0
|
||||
while (ei < nr_elems) {
|
||||
elem_slot = instr[3 + ei]
|
||||
emit(` call $JS_SetPropertyNumber(l %ctx, l ${s(a1)}, l ${text(ei)}, l ${s(elem_slot)})`)
|
||||
ei = ei + 1
|
||||
}
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
|
||||
// --- Array push/pop ---
|
||||
|
||||
if (op == "push") {
|
||||
emit(` call $cell_rt_push(l %ctx, l ${s(a1)}, l ${s(a2)})`)
|
||||
continue
|
||||
}
|
||||
if (op == "pop") {
|
||||
emit(` ${s(a1)} =l call $cell_rt_pop(l %ctx, l ${s(a2)})`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
|
||||
// --- Misc ---
|
||||
|
||||
if (op == "return") {
|
||||
emit(` ret ${s(a1)}`)
|
||||
continue
|
||||
}
|
||||
if (op == "disrupt") {
|
||||
emit(` call $cell_rt_disrupt(l %ctx)`)
|
||||
emit(` ret ${text(qbe.js_null)}`)
|
||||
continue
|
||||
}
|
||||
if (op == "delete") {
|
||||
emit(` ${s(a1)} =l call $cell_rt_delete(l %ctx, l ${s(a2)}, l ${s(a3)})`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
if (op == "typeof") {
|
||||
emit(` ${s(a1)} =l call $cell_rt_typeof(l %ctx, l ${s(a2)})`)
|
||||
wb(a1)
|
||||
continue
|
||||
}
|
||||
|
||||
// --- Unknown opcode ---
|
||||
emit(` # unknown: ${op}`)
|
||||
}
|
||||
|
||||
emit("}")
|
||||
emit("")
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// Main: compile all functions then main
|
||||
// ============================================================
|
||||
|
||||
var fi = 0
|
||||
while (fi < length(ir.functions)) {
|
||||
compile_fn(ir.functions[fi], fi, false)
|
||||
fi = fi + 1
|
||||
}
|
||||
|
||||
compile_fn(ir.main, -1, true)
|
||||
|
||||
// Assemble: data section first, then function bodies
|
||||
var result = []
|
||||
var di = 0
|
||||
while (di < length(data_out)) {
|
||||
push(result, data_out[di])
|
||||
di = di + 1
|
||||
}
|
||||
if (length(data_out) > 0) push(result, "")
|
||||
|
||||
di = 0
|
||||
while (di < length(out)) {
|
||||
push(result, out[di])
|
||||
di = di + 1
|
||||
}
|
||||
|
||||
return text(result, "\n")
|
||||
}
|
||||
|
||||
return qbe_emit
|
||||
BIN
qbe_emit.mach
Normal file
BIN
qbe_emit.mach
Normal file
Binary file not shown.
82
qbe_rt.c
Normal file
82
qbe_rt.c
Normal file
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* qbe_rt.c - Non-inline wrappers for QBE-compiled ƿit modules
|
||||
*
|
||||
* Provides non-inline versions of static-inline quickjs functions
|
||||
* (which QBE-generated code calls as external symbols).
|
||||
*
|
||||
* All cell_rt_* runtime functions are implemented in source/qbe_helpers.c
|
||||
* (compiled into the cell binary) and resolved via -undefined dynamic_lookup.
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
|
||||
typedef uint64_t JSValue;
|
||||
typedef struct JSContext JSContext;
|
||||
|
||||
#define JS_TAG_SHORT_FLOAT 5
|
||||
#define JS_TAG_NULL 7
|
||||
#define JS_VAL_NULL 7
|
||||
|
||||
/* ============================================================
|
||||
Non-inline wrappers for static-inline quickjs functions
|
||||
============================================================ */
|
||||
|
||||
/*
|
||||
* __JS_NewFloat64 — encode double as tagged JSValue
|
||||
* Short float: [sign:1][exp:8][mantissa:52][tag:3]
|
||||
* Returns tagged int if value is an exact integer in int32 range
|
||||
*/
|
||||
JSValue __JS_NewFloat64(JSContext *ctx, double d) {
|
||||
union { double d; uint64_t u; } u;
|
||||
u.d = d;
|
||||
|
||||
uint64_t sign = u.u >> 63;
|
||||
int exp = (u.u >> 52) & 0x7FF;
|
||||
uint64_t mantissa = u.u & ((1ULL << 52) - 1);
|
||||
|
||||
/* Zero */
|
||||
if (exp == 0 && mantissa == 0)
|
||||
return JS_TAG_SHORT_FLOAT;
|
||||
/* NaN/Inf → null */
|
||||
if (exp == 0x7FF)
|
||||
return JS_VAL_NULL;
|
||||
/* Subnormals → zero */
|
||||
if (exp == 0)
|
||||
return (sign << 63) | JS_TAG_SHORT_FLOAT;
|
||||
|
||||
int short_exp = exp - 1023 + 127;
|
||||
if (short_exp < 1 || short_exp > 254)
|
||||
return JS_VAL_NULL;
|
||||
|
||||
/* Prefer integer if exact */
|
||||
if (d >= (double)(-2147483647 - 1) && d <= (double)2147483647) {
|
||||
int32_t i = (int32_t)d;
|
||||
if ((double)i == d)
|
||||
return (uint64_t)(uint32_t)i << 1;
|
||||
}
|
||||
|
||||
return (sign << 63)
|
||||
| ((uint64_t)short_exp << 55)
|
||||
| (mantissa << 3)
|
||||
| JS_TAG_SHORT_FLOAT;
|
||||
}
|
||||
|
||||
/*
|
||||
* JS_IsNumber — check if value is tagged int or short float
|
||||
*/
|
||||
int JS_IsNumber(JSValue v) {
|
||||
int is_int = (v & 1) == 0;
|
||||
int is_float = (v & 7) == JS_TAG_SHORT_FLOAT;
|
||||
return is_int || is_float;
|
||||
}
|
||||
|
||||
/*
|
||||
* JS_NewString — create string from C string (wraps JS_NewStringLen)
|
||||
*/
|
||||
extern JSValue JS_NewStringLen(JSContext *ctx, const char *str, size_t len);
|
||||
|
||||
JSValue JS_NewString(JSContext *ctx, const char *str) {
|
||||
return JS_NewStringLen(ctx, str, strlen(str));
|
||||
}
|
||||
6
qop.c
6
qop.c
@@ -267,7 +267,7 @@ static JSValue js_qop_list(JSContext *js, JSValue self, int argc, JSValue *argv)
|
||||
|
||||
JSValue str = JS_NewStringLen(js, path, len - 1); // -1 for null terminator
|
||||
js_free(js, path);
|
||||
JS_SetPropertyUint32(js, arr, count++, str);
|
||||
JS_SetPropertyNumber(js, arr, count++, str);
|
||||
}
|
||||
|
||||
return arr;
|
||||
@@ -458,13 +458,13 @@ static const JSCFunctionListEntry js_qop_funcs[] = {
|
||||
|
||||
JSValue js_qop_use(JSContext *js) {
|
||||
JS_NewClassID(&js_qop_archive_class_id);
|
||||
JS_NewClass(JS_GetRuntime(js), js_qop_archive_class_id, &js_qop_archive_class);
|
||||
JS_NewClass(js, js_qop_archive_class_id, &js_qop_archive_class);
|
||||
JSValue archive_proto = JS_NewObject(js);
|
||||
JS_SetPropertyFunctionList(js, archive_proto, js_qop_archive_funcs, countof(js_qop_archive_funcs));
|
||||
JS_SetClassProto(js, js_qop_archive_class_id, archive_proto);
|
||||
|
||||
JS_NewClassID(&js_qop_writer_class_id);
|
||||
JS_NewClass(JS_GetRuntime(js), js_qop_writer_class_id, &js_qop_writer_class);
|
||||
JS_NewClass(js, js_qop_writer_class_id, &js_qop_writer_class);
|
||||
JSValue writer_proto = JS_NewObject(js);
|
||||
JS_SetPropertyFunctionList(js, writer_proto, js_qop_writer_funcs, countof(js_qop_writer_funcs));
|
||||
JS_SetClassProto(js, js_qop_writer_class_id, writer_proto);
|
||||
|
||||
75
regen.cm
Normal file
75
regen.cm
Normal file
@@ -0,0 +1,75 @@
|
||||
// regen.cm — regenerate .mach bytecode files
|
||||
// Run with: ./cell --core . regen.cm
|
||||
|
||||
var fd = use("fd")
|
||||
var json = use("json")
|
||||
var tokenize = use("tokenize")
|
||||
var parse = use("parse")
|
||||
var fold = use("fold")
|
||||
var mcode = use("mcode")
|
||||
var streamline = use("streamline")
|
||||
|
||||
var files = [
|
||||
{src: "tokenize.cm", name: "tokenize", out: "tokenize.mach"},
|
||||
{src: "parse.cm", name: "parse", out: "parse.mach"},
|
||||
{src: "fold.cm", name: "fold", out: "fold.mach"},
|
||||
{src: "mcode.cm", name: "mcode", out: "mcode.mach"},
|
||||
{src: "streamline.cm", name: "streamline", out: "streamline.mach"},
|
||||
{src: "qbe.cm", name: "qbe", out: "qbe.mach"},
|
||||
{src: "qbe_emit.cm", name: "qbe_emit", out: "qbe_emit.mach"},
|
||||
{src: "internal/bootstrap.cm", name: "bootstrap", out: "internal/bootstrap.mach"},
|
||||
{src: "internal/engine.cm", name: "engine", out: "internal/engine.mach"}
|
||||
]
|
||||
|
||||
var i = 0
|
||||
var entry = null
|
||||
var src = null
|
||||
var tok_result = null
|
||||
var ast = null
|
||||
var folded = null
|
||||
var compiled = null
|
||||
var optimized = null
|
||||
var mcode_json = null
|
||||
var bytecode = null
|
||||
var f = null
|
||||
var errs = null
|
||||
var ei = 0
|
||||
var e = null
|
||||
var had_errors = false
|
||||
|
||||
while (i < length(files)) {
|
||||
entry = files[i]
|
||||
src = text(fd.slurp(entry.src))
|
||||
tok_result = tokenize(src, entry.src)
|
||||
ast = parse(tok_result.tokens, src, entry.src, tokenize)
|
||||
// Check for parse/semantic errors
|
||||
errs = ast.errors
|
||||
if (errs != null && length(errs) > 0) {
|
||||
ei = 0
|
||||
while (ei < length(errs)) {
|
||||
e = errs[ei]
|
||||
if (e.line != null) {
|
||||
print(`${entry.src}:${text(e.line)}:${text(e.column)}: error: ${e.message}`)
|
||||
} else {
|
||||
print(`${entry.src}: error: ${e.message}`)
|
||||
}
|
||||
ei = ei + 1
|
||||
}
|
||||
had_errors = true
|
||||
i = i + 1
|
||||
continue
|
||||
}
|
||||
folded = fold(ast)
|
||||
compiled = mcode(folded)
|
||||
optimized = streamline(compiled)
|
||||
mcode_json = json.encode(optimized)
|
||||
bytecode = mach_compile_mcode_bin(entry.name, mcode_json)
|
||||
f = fd.open(entry.out, "w")
|
||||
fd.write(f, bytecode)
|
||||
fd.close(f)
|
||||
print(`wrote ${entry.out}`)
|
||||
i = i + 1
|
||||
}
|
||||
if (had_errors) {
|
||||
print("regen aborted: fix errors above")
|
||||
}
|
||||
91
run_native.ce
Normal file
91
run_native.ce
Normal file
@@ -0,0 +1,91 @@
|
||||
// run_native.ce — load a module both interpreted and native, compare speed
|
||||
//
|
||||
// Usage:
|
||||
// cell --core . run_native.ce <module>
|
||||
//
|
||||
// Loads <module>.cm via use() (interpreted) and <module>.dylib (native),
|
||||
// runs both and compares results and timing.
|
||||
|
||||
var os = use('os')
|
||||
|
||||
if (length(args) < 1) {
|
||||
print('usage: cell --core . run_native.ce <module>')
|
||||
print(' e.g. cell --core . run_native.ce num_torture')
|
||||
return
|
||||
}
|
||||
|
||||
var name = args[0]
|
||||
if (ends_with(name, '.cm')) {
|
||||
name = text(name, 0, length(name) - 3)
|
||||
}
|
||||
|
||||
var safe = replace(replace(name, '/', '_'), '-', '_')
|
||||
var symbol = 'js_' + safe + '_use'
|
||||
var dylib_path = './' + name + '.dylib'
|
||||
var fd = use('fd')
|
||||
|
||||
// --- Test argument for function-returning modules ---
|
||||
var test_arg = 5000000
|
||||
if (length(args) > 1) {
|
||||
test_arg = number(args[1])
|
||||
}
|
||||
|
||||
// --- Interpreted run ---
|
||||
print('--- interpreted ---')
|
||||
var t1 = os.now()
|
||||
var mod_interp = use(name)
|
||||
var t2 = os.now()
|
||||
var result_interp = null
|
||||
if (is_function(mod_interp)) {
|
||||
print('module returns a function, calling with ' + text(test_arg))
|
||||
t1 = os.now()
|
||||
result_interp = mod_interp(test_arg)
|
||||
t2 = os.now()
|
||||
}
|
||||
result_interp = result_interp != null ? result_interp : mod_interp
|
||||
var ms_interp = (t2 - t1) / 1000000
|
||||
print('result: ' + text(result_interp))
|
||||
print('time: ' + text(ms_interp) + ' ms')
|
||||
|
||||
// --- Native run ---
|
||||
if (!fd.is_file(dylib_path)) {
|
||||
print('\nno ' + dylib_path + ' found — run compile.ce first')
|
||||
return
|
||||
}
|
||||
|
||||
print('\n--- native ---')
|
||||
var t3 = os.now()
|
||||
var lib = os.dylib_open(dylib_path)
|
||||
var t4 = os.now()
|
||||
var mod_native = os.dylib_symbol(lib, symbol)
|
||||
var t5 = os.now()
|
||||
var result_native = null
|
||||
if (is_function(mod_native)) {
|
||||
print('module returns a function, calling with ' + text(test_arg))
|
||||
t4 = os.now()
|
||||
result_native = mod_native(test_arg)
|
||||
t5 = os.now()
|
||||
}
|
||||
result_native = result_native != null ? result_native : mod_native
|
||||
var ms_load = (t4 - t3) / 1000000
|
||||
var ms_exec = (t5 - t4) / 1000000
|
||||
var ms_native = (t5 - t3) / 1000000
|
||||
print('result: ' + text(result_native))
|
||||
print('load: ' + text(ms_load) + ' ms')
|
||||
print('exec: ' + text(ms_exec) + ' ms')
|
||||
print('total: ' + text(ms_native) + ' ms')
|
||||
|
||||
// --- Comparison ---
|
||||
print('\n--- comparison ---')
|
||||
var match = result_interp == result_native
|
||||
var speedup = 0
|
||||
var speedup_exec = 0
|
||||
print('match: ' + text(match))
|
||||
if (ms_native > 0) {
|
||||
speedup = ms_interp / ms_native
|
||||
print('speedup: ' + text(speedup) + 'x (total)')
|
||||
}
|
||||
if (ms_exec > 0) {
|
||||
speedup_exec = ms_interp / ms_exec
|
||||
print('speedup: ' + text(speedup_exec) + 'x (exec only)')
|
||||
}
|
||||
3191
source/cJSON.c
Normal file
3191
source/cJSON.c
Normal file
File diff suppressed because it is too large
Load Diff
306
source/cJSON.h
Normal file
306
source/cJSON.h
Normal file
@@ -0,0 +1,306 @@
|
||||
/*
|
||||
Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef cJSON__h
|
||||
#define cJSON__h
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#if !defined(__WINDOWS__) && (defined(WIN32) || defined(WIN64) || defined(_MSC_VER) || defined(_WIN32))
|
||||
#define __WINDOWS__
|
||||
#endif
|
||||
|
||||
#ifdef __WINDOWS__
|
||||
|
||||
/* When compiling for windows, we specify a specific calling convention to avoid issues where we are being called from a project with a different default calling convention. For windows you have 3 define options:
|
||||
|
||||
CJSON_HIDE_SYMBOLS - Define this in the case where you don't want to ever dllexport symbols
|
||||
CJSON_EXPORT_SYMBOLS - Define this on library build when you want to dllexport symbols (default)
|
||||
CJSON_IMPORT_SYMBOLS - Define this if you want to dllimport symbol
|
||||
|
||||
For *nix builds that support visibility attribute, you can define similar behavior by
|
||||
|
||||
setting default visibility to hidden by adding
|
||||
-fvisibility=hidden (for gcc)
|
||||
or
|
||||
-xldscope=hidden (for sun cc)
|
||||
to CFLAGS
|
||||
|
||||
then using the CJSON_API_VISIBILITY flag to "export" the same symbols the way CJSON_EXPORT_SYMBOLS does
|
||||
|
||||
*/
|
||||
|
||||
#define CJSON_CDECL __cdecl
|
||||
#define CJSON_STDCALL __stdcall
|
||||
|
||||
/* export symbols by default, this is necessary for copy pasting the C and header file */
|
||||
#if !defined(CJSON_HIDE_SYMBOLS) && !defined(CJSON_IMPORT_SYMBOLS) && !defined(CJSON_EXPORT_SYMBOLS)
|
||||
#define CJSON_EXPORT_SYMBOLS
|
||||
#endif
|
||||
|
||||
#if defined(CJSON_HIDE_SYMBOLS)
|
||||
#define CJSON_PUBLIC(type) type CJSON_STDCALL
|
||||
#elif defined(CJSON_EXPORT_SYMBOLS)
|
||||
#define CJSON_PUBLIC(type) __declspec(dllexport) type CJSON_STDCALL
|
||||
#elif defined(CJSON_IMPORT_SYMBOLS)
|
||||
#define CJSON_PUBLIC(type) __declspec(dllimport) type CJSON_STDCALL
|
||||
#endif
|
||||
#else /* !__WINDOWS__ */
|
||||
#define CJSON_CDECL
|
||||
#define CJSON_STDCALL
|
||||
|
||||
#if (defined(__GNUC__) || defined(__SUNPRO_CC) || defined (__SUNPRO_C)) && defined(CJSON_API_VISIBILITY)
|
||||
#define CJSON_PUBLIC(type) __attribute__((visibility("default"))) type
|
||||
#else
|
||||
#define CJSON_PUBLIC(type) type
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/* project version */
|
||||
#define CJSON_VERSION_MAJOR 1
|
||||
#define CJSON_VERSION_MINOR 7
|
||||
#define CJSON_VERSION_PATCH 19
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
/* cJSON Types: */
|
||||
#define cJSON_Invalid (0)
|
||||
#define cJSON_False (1 << 0)
|
||||
#define cJSON_True (1 << 1)
|
||||
#define cJSON_NULL (1 << 2)
|
||||
#define cJSON_Number (1 << 3)
|
||||
#define cJSON_String (1 << 4)
|
||||
#define cJSON_Array (1 << 5)
|
||||
#define cJSON_Object (1 << 6)
|
||||
#define cJSON_Raw (1 << 7) /* raw json */
|
||||
|
||||
#define cJSON_IsReference 256
|
||||
#define cJSON_StringIsConst 512
|
||||
|
||||
/* The cJSON structure: */
|
||||
typedef struct cJSON
|
||||
{
|
||||
/* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
|
||||
struct cJSON *next;
|
||||
struct cJSON *prev;
|
||||
/* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
|
||||
struct cJSON *child;
|
||||
|
||||
/* The type of the item, as above. */
|
||||
int type;
|
||||
|
||||
/* The item's string, if type==cJSON_String and type == cJSON_Raw */
|
||||
char *valuestring;
|
||||
/* writing to valueint is DEPRECATED, use cJSON_SetNumberValue instead */
|
||||
int valueint;
|
||||
/* The item's number, if type==cJSON_Number */
|
||||
double valuedouble;
|
||||
|
||||
/* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
|
||||
char *string;
|
||||
} cJSON;
|
||||
|
||||
typedef struct cJSON_Hooks
|
||||
{
|
||||
/* malloc/free are CDECL on Windows regardless of the default calling convention of the compiler, so ensure the hooks allow passing those functions directly. */
|
||||
void *(CJSON_CDECL *malloc_fn)(size_t sz);
|
||||
void (CJSON_CDECL *free_fn)(void *ptr);
|
||||
} cJSON_Hooks;
|
||||
|
||||
typedef int cJSON_bool;
|
||||
|
||||
/* Limits how deeply nested arrays/objects can be before cJSON rejects to parse them.
|
||||
* This is to prevent stack overflows. */
|
||||
#ifndef CJSON_NESTING_LIMIT
|
||||
#define CJSON_NESTING_LIMIT 1000
|
||||
#endif
|
||||
|
||||
/* Limits the length of circular references can be before cJSON rejects to parse them.
|
||||
* This is to prevent stack overflows. */
|
||||
#ifndef CJSON_CIRCULAR_LIMIT
|
||||
#define CJSON_CIRCULAR_LIMIT 10000
|
||||
#endif
|
||||
|
||||
/* returns the version of cJSON as a string */
|
||||
CJSON_PUBLIC(const char*) cJSON_Version(void);
|
||||
|
||||
/* Supply malloc, realloc and free functions to cJSON */
|
||||
CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks);
|
||||
|
||||
/* Memory Management: the caller is always responsible to free the results from all variants of cJSON_Parse (with cJSON_Delete) and cJSON_Print (with stdlib free, cJSON_Hooks.free_fn, or cJSON_free as appropriate). The exception is cJSON_PrintPreallocated, where the caller has full responsibility of the buffer. */
|
||||
/* Supply a block of JSON, and this returns a cJSON object you can interrogate. */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_ParseWithLength(const char *value, size_t buffer_length);
|
||||
/* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve the pointer to the final byte parsed. */
|
||||
/* If you supply a ptr in return_parse_end and parsing fails, then return_parse_end will contain a pointer to the error so will match cJSON_GetErrorPtr(). */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_ParseWithLengthOpts(const char *value, size_t buffer_length, const char **return_parse_end, cJSON_bool require_null_terminated);
|
||||
|
||||
/* Render a cJSON entity to text for transfer/storage. */
|
||||
CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item);
|
||||
/* Render a cJSON entity to text for transfer/storage without any formatting. */
|
||||
CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item);
|
||||
/* Render a cJSON entity to text using a buffered strategy. prebuffer is a guess at the final size. guessing well reduces reallocation. fmt=0 gives unformatted, =1 gives formatted */
|
||||
CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt);
|
||||
/* Render a cJSON entity to text using a buffer already allocated in memory with given length. Returns 1 on success and 0 on failure. */
|
||||
/* NOTE: cJSON is not always 100% accurate in estimating how much memory it will use, so to be safe allocate 5 bytes more than you actually need */
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format);
|
||||
/* Delete a cJSON entity and all subentities. */
|
||||
CJSON_PUBLIC(void) cJSON_Delete(cJSON *item);
|
||||
|
||||
/* Returns the number of items in an array (or object). */
|
||||
CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array);
|
||||
/* Retrieve item number "index" from array "array". Returns NULL if unsuccessful. */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index);
|
||||
/* Get item "string" from object. Case insensitive. */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string);
|
||||
/* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */
|
||||
CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void);
|
||||
|
||||
/* Check item type and return its value */
|
||||
CJSON_PUBLIC(char *) cJSON_GetStringValue(const cJSON * const item);
|
||||
CJSON_PUBLIC(double) cJSON_GetNumberValue(const cJSON * const item);
|
||||
|
||||
/* These functions check the type of an item */
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item);
|
||||
|
||||
/* These calls create a cJSON item of the appropriate type. */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool boolean);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string);
|
||||
/* raw json */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void);
|
||||
|
||||
/* Create a string where valuestring references a string so
|
||||
* it will not be freed by cJSON_Delete */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateStringReference(const char *string);
|
||||
/* Create an object/array that only references it's elements so
|
||||
* they will not be freed by cJSON_Delete */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateObjectReference(const cJSON *child);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateArrayReference(const cJSON *child);
|
||||
|
||||
/* These utilities create an Array of count items.
|
||||
* The parameter count cannot be greater than the number of elements in the number array, otherwise array access will be out of bounds.*/
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char *const *strings, int count);
|
||||
|
||||
/* Append item to the specified array/object. */
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item);
|
||||
/* Use this when string is definitely const (i.e. a literal, or as good as), and will definitely survive the cJSON object.
|
||||
* WARNING: When this function was used, make sure to always check that (item->type & cJSON_StringIsConst) is zero before
|
||||
* writing to `item->string` */
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item);
|
||||
/* Append reference to item to the specified array/object. Use this when you want to add an existing cJSON to a new cJSON, but don't want to corrupt your existing cJSON. */
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item);
|
||||
|
||||
/* Remove/Detach items from Arrays/Objects. */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which);
|
||||
CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string);
|
||||
CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string);
|
||||
CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string);
|
||||
|
||||
/* Update array items. */
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem); /* Shifts pre-existing items to the right. */
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object,const char *string,cJSON *newitem);
|
||||
|
||||
/* Duplicate a cJSON item */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse);
|
||||
/* Duplicate will create a new, identical cJSON item to the one you pass, in new memory that will
|
||||
* need to be released. With recurse!=0, it will duplicate any children connected to the item.
|
||||
* The item->next and ->prev pointers are always zero on return from Duplicate. */
|
||||
/* Recursively compare two cJSON items for equality. If either a or b is NULL or invalid, they will be considered unequal.
|
||||
* case_sensitive determines if object keys are treated case sensitive (1) or case insensitive (0) */
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * const b, const cJSON_bool case_sensitive);
|
||||
|
||||
/* Minify a strings, remove blank characters(such as ' ', '\t', '\r', '\n') from strings.
|
||||
* The input pointer json cannot point to a read-only address area, such as a string constant,
|
||||
* but should point to a readable and writable address area. */
|
||||
CJSON_PUBLIC(void) cJSON_Minify(char *json);
|
||||
|
||||
/* Helper functions for creating and adding items to an object at the same time.
|
||||
* They return the added item or NULL on failure. */
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, const char * const name);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, const char * const name);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * const name);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddBoolToObject(cJSON * const object, const char * const name, const cJSON_bool boolean);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * const name, const double number);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON * const object, const char * const name, const char * const raw);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddObjectToObject(cJSON * const object, const char * const name);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, const char * const name);
|
||||
|
||||
/* When assigning an integer value, it needs to be propagated to valuedouble too. */
|
||||
#define cJSON_SetIntValue(object, number) ((object) ? (object)->valueint = (object)->valuedouble = (number) : (number))
|
||||
/* helper for the cJSON_SetNumberValue macro */
|
||||
CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number);
|
||||
#define cJSON_SetNumberValue(object, number) ((object != NULL) ? cJSON_SetNumberHelper(object, (double)number) : (number))
|
||||
/* Change the valuestring of a cJSON_String object, only takes effect when type of object is cJSON_String */
|
||||
CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring);
|
||||
|
||||
/* If the object is not a boolean type this does nothing and returns cJSON_Invalid else it returns the new type*/
|
||||
#define cJSON_SetBoolValue(object, boolValue) ( \
|
||||
(object != NULL && ((object)->type & (cJSON_False|cJSON_True))) ? \
|
||||
(object)->type=((object)->type &(~(cJSON_False|cJSON_True)))|((boolValue)?cJSON_True:cJSON_False) : \
|
||||
cJSON_Invalid\
|
||||
)
|
||||
|
||||
/* Macro for iterating over an array or object */
|
||||
#define cJSON_ArrayForEach(element, array) for(element = (array != NULL) ? (array)->child : NULL; element != NULL; element = element->next)
|
||||
|
||||
/* malloc/free objects using the malloc/free functions that have been set with cJSON_InitHooks */
|
||||
CJSON_PUBLIC(void *) cJSON_malloc(size_t size);
|
||||
CJSON_PUBLIC(void) cJSON_free(void *object);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
541
source/cell.c
541
source/cell.c
@@ -2,7 +2,6 @@
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
#define WOTA_IMPLEMENTATION
|
||||
#include "wota.h"
|
||||
|
||||
#define STB_DS_IMPLEMENTATION
|
||||
@@ -10,11 +9,14 @@
|
||||
|
||||
#include "cell.h"
|
||||
#include "cell_internal.h"
|
||||
#include "cJSON.h"
|
||||
|
||||
#define ENGINE "internal/engine.cm"
|
||||
#define BOOTSTRAP_MACH "internal/bootstrap.mach"
|
||||
#define BOOTSTRAP_SRC "internal/bootstrap.cm"
|
||||
#define CELL_SHOP_DIR ".cell"
|
||||
#define CELL_CORE_DIR "packages/core"
|
||||
|
||||
#include <math.h>
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
@@ -24,7 +26,9 @@ int run_c_test_suite(JSContext *ctx);
|
||||
static int run_test_suite(size_t heap_size);
|
||||
|
||||
cell_rt *root_cell = NULL;
|
||||
static char *shop_path = NULL;
|
||||
static char *core_path = NULL;
|
||||
static JSRuntime *g_runtime = NULL;
|
||||
|
||||
// Get the home directory
|
||||
static const char* get_home_dir(void) {
|
||||
@@ -35,31 +39,57 @@ static const char* get_home_dir(void) {
|
||||
return home;
|
||||
}
|
||||
|
||||
// Find and verify the cell shop at ~/.cell
|
||||
int find_cell_shop(void)
|
||||
// Resolve shop_path and core_path
|
||||
// core: --core flag > CELL_CORE env > derived from shop
|
||||
// shop: --shop flag > CELL_SHOP env > ~/.cell
|
||||
int find_cell_shop(const char *shop_override, const char *core_override)
|
||||
{
|
||||
const char *home = get_home_dir();
|
||||
if (!home) {
|
||||
printf("ERROR: Could not determine home directory. Set HOME environment variable.\n");
|
||||
return 0;
|
||||
// Resolve shop_path
|
||||
if (shop_override) {
|
||||
shop_path = strdup(shop_override);
|
||||
} else {
|
||||
const char *env = getenv("CELL_SHOP");
|
||||
if (env) {
|
||||
shop_path = strdup(env);
|
||||
} else {
|
||||
const char *home = get_home_dir();
|
||||
if (!home && !core_override && !getenv("CELL_CORE")) {
|
||||
printf("ERROR: Could not determine home directory. Set HOME environment variable.\n");
|
||||
return 0;
|
||||
}
|
||||
if (home) {
|
||||
size_t path_len = strlen(home) + strlen("/" CELL_SHOP_DIR) + 1;
|
||||
shop_path = malloc(path_len);
|
||||
if (shop_path)
|
||||
snprintf(shop_path, path_len, "%s/" CELL_SHOP_DIR, home);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Resolve core_path
|
||||
if (core_override) {
|
||||
core_path = strdup(core_override);
|
||||
} else {
|
||||
const char *env = getenv("CELL_CORE");
|
||||
if (env) {
|
||||
core_path = strdup(env);
|
||||
} else if (shop_path) {
|
||||
size_t core_len = strlen(shop_path) + strlen("/" CELL_CORE_DIR) + 1;
|
||||
core_path = malloc(core_len);
|
||||
if (core_path)
|
||||
snprintf(core_path, core_len, "%s/" CELL_CORE_DIR, shop_path);
|
||||
}
|
||||
}
|
||||
|
||||
// Build path to ~/.cell/core
|
||||
size_t path_len = strlen(home) + strlen("/" CELL_SHOP_DIR "/" CELL_CORE_DIR) + 1;
|
||||
core_path = malloc(path_len);
|
||||
if (!core_path) {
|
||||
printf("ERROR: Could not allocate memory for core path\n");
|
||||
printf("ERROR: No core path. Use --core <path> or set CELL_CORE.\n");
|
||||
return 0;
|
||||
}
|
||||
snprintf(core_path, path_len, "%s/" CELL_SHOP_DIR "/" CELL_CORE_DIR, home);
|
||||
|
||||
// Check if the core directory exists
|
||||
struct stat st;
|
||||
if (stat(core_path, &st) != 0 || !S_ISDIR(st.st_mode)) {
|
||||
printf("ERROR: Cell shop not found at %s/" CELL_SHOP_DIR "\n", home);
|
||||
printf("Run 'cell install' to set up the cell environment.\n");
|
||||
free(core_path);
|
||||
core_path = NULL;
|
||||
printf("ERROR: Core not found at %s\n", core_path);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -69,37 +99,37 @@ int find_cell_shop(void)
|
||||
// Load a file from the core directory
|
||||
static char* load_core_file(const char *filename, size_t *out_size) {
|
||||
if (!core_path) return NULL;
|
||||
|
||||
|
||||
size_t path_len = strlen(core_path) + 1 + strlen(filename) + 1;
|
||||
char *full_path = malloc(path_len);
|
||||
if (!full_path) return NULL;
|
||||
|
||||
|
||||
snprintf(full_path, path_len, "%s/%s", core_path, filename);
|
||||
|
||||
|
||||
FILE *fh = fopen(full_path, "rb");
|
||||
free(full_path);
|
||||
|
||||
|
||||
if (!fh) return NULL;
|
||||
|
||||
|
||||
fseek(fh, 0, SEEK_END);
|
||||
long file_size = ftell(fh);
|
||||
fseek(fh, 0, SEEK_SET);
|
||||
|
||||
|
||||
char *data = malloc(file_size + 1);
|
||||
if (!data) {
|
||||
fclose(fh);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
if (fread(data, 1, file_size, fh) != (size_t)file_size) {
|
||||
free(data);
|
||||
fclose(fh);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
fclose(fh);
|
||||
data[file_size] = 0;
|
||||
|
||||
|
||||
if (out_size) *out_size = file_size;
|
||||
return data;
|
||||
}
|
||||
@@ -118,69 +148,78 @@ void actor_disrupt(cell_rt *crt)
|
||||
|
||||
JSValue js_os_use(JSContext *js);
|
||||
JSValue js_math_use(JSContext *js);
|
||||
JSValue js_json_use(JSContext *js);
|
||||
JSValue js_nota_use(JSContext *js);
|
||||
JSValue js_wota_use(JSContext *js);
|
||||
|
||||
void script_startup(cell_rt *prt)
|
||||
{
|
||||
JSRuntime *rt;
|
||||
|
||||
rt = JS_NewRuntime();
|
||||
|
||||
JSContext *js = JS_NewContextRaw(rt);
|
||||
JS_SetInterruptHandler(rt, (JSInterruptHandler *)actor_interrupt_cb, prt);
|
||||
|
||||
JS_AddIntrinsicBaseObjects(js);
|
||||
JS_AddIntrinsicEval(js);
|
||||
JS_AddIntrinsicRegExp(js);
|
||||
JS_AddIntrinsicJSON(js);
|
||||
if (!g_runtime) {
|
||||
g_runtime = JS_NewRuntime();
|
||||
}
|
||||
JSContext *js = JS_NewContext(g_runtime);
|
||||
JS_SetInterruptHandler(js, (JSInterruptHandler *)actor_interrupt_cb, prt);
|
||||
|
||||
JS_SetContextOpaque(js, prt);
|
||||
prt->context = js;
|
||||
|
||||
/* Register all GCRef fields so the Cheney GC can relocate them. */
|
||||
JS_AddGCRef(js, &prt->idx_buffer_ref);
|
||||
JS_AddGCRef(js, &prt->on_exception_ref);
|
||||
JS_AddGCRef(js, &prt->message_handle_ref);
|
||||
JS_AddGCRef(js, &prt->unneeded_ref);
|
||||
JS_AddGCRef(js, &prt->actor_sym_ref);
|
||||
prt->idx_buffer_ref.val = JS_NULL;
|
||||
prt->on_exception_ref.val = JS_NULL;
|
||||
prt->message_handle_ref.val = JS_NULL;
|
||||
prt->unneeded_ref.val = JS_NULL;
|
||||
prt->actor_sym_ref.val = JS_NULL;
|
||||
|
||||
cell_rt *crt = JS_GetContextOpaque(js);
|
||||
JS_FreeValue(js, js_blob_use(js));
|
||||
|
||||
JSValue globalThis = JS_GetGlobalObject(js);
|
||||
|
||||
JSValue cell = JS_NewObject(js);
|
||||
JS_SetPropertyStr(js,globalThis,"cell", cell);
|
||||
|
||||
JSValue hidden_fn = JS_NewObject(js);
|
||||
|
||||
JS_SetPropertyStr(js, cell, "hidden", hidden_fn);
|
||||
JS_SetPropertyStr(js, hidden_fn, "os", js_os_use(js));
|
||||
|
||||
crt->actor_sym = JS_NewObject(js);
|
||||
JS_SetPropertyStr(js, hidden_fn, "actorsym", JS_DupValue(js,crt->actor_sym));
|
||||
|
||||
if (crt->init_wota) {
|
||||
JS_SetPropertyStr(js, hidden_fn, "init", wota2value(js, crt->init_wota));
|
||||
// init wota can now be freed
|
||||
free(crt->init_wota);
|
||||
crt->init_wota = NULL;
|
||||
}
|
||||
|
||||
// Store the core path for scripts to use
|
||||
JSValue js_cell = JS_GetPropertyStr(js, globalThis, "cell");
|
||||
JSValue hidden = JS_GetPropertyStr(js, js_cell, "hidden");
|
||||
if (core_path) {
|
||||
JS_SetPropertyStr(js, hidden, "core_path", JS_NewString(js, core_path));
|
||||
}
|
||||
JS_FreeValue(js, hidden);
|
||||
JS_FreeValue(js, js_cell);
|
||||
|
||||
JS_FreeValue(js, globalThis);
|
||||
|
||||
// Load engine.cm from the core directory
|
||||
size_t engine_size;
|
||||
char *data = load_core_file(ENGINE, &engine_size);
|
||||
if (!data) {
|
||||
printf("ERROR: Could not load %s from %s!\n", ENGINE, core_path);
|
||||
// Load pre-compiled bootstrap bytecode (.mach)
|
||||
size_t boot_size;
|
||||
char *boot_data = load_core_file(BOOTSTRAP_MACH, &boot_size);
|
||||
if (!boot_data) {
|
||||
printf("ERROR: Could not load bootstrap from %s!\n", core_path);
|
||||
return;
|
||||
}
|
||||
|
||||
// Create hidden environment
|
||||
JSValue hidden_env = JS_NewObject(js);
|
||||
JS_SetPropertyStr(js, hidden_env, "os", js_os_use(js));
|
||||
JS_SetPropertyStr(js, hidden_env, "json", js_json_use(js));
|
||||
JS_SetPropertyStr(js, hidden_env, "nota", js_nota_use(js));
|
||||
JS_SetPropertyStr(js, hidden_env, "wota", js_wota_use(js));
|
||||
|
||||
crt->actor_sym_ref.val = JS_NewObject(js);
|
||||
JS_SetPropertyStr(js, hidden_env, "actorsym", JS_DupValue(js, crt->actor_sym_ref.val));
|
||||
|
||||
// Always set init (even if null)
|
||||
if (crt->init_wota) {
|
||||
JS_SetPropertyStr(js, hidden_env, "init", wota2value(js, crt->init_wota));
|
||||
free(crt->init_wota);
|
||||
crt->init_wota = NULL;
|
||||
} else {
|
||||
JS_SetPropertyStr(js, hidden_env, "init", JS_NULL);
|
||||
}
|
||||
|
||||
// Set args to null for actor spawn (not CLI mode)
|
||||
JS_SetPropertyStr(js, hidden_env, "args", JS_NULL);
|
||||
|
||||
if (core_path)
|
||||
JS_SetPropertyStr(js, hidden_env, "core_path", JS_NewString(js, core_path));
|
||||
JS_SetPropertyStr(js, hidden_env, "shop_path",
|
||||
shop_path ? JS_NewString(js, shop_path) : JS_NULL);
|
||||
|
||||
// Stone the environment
|
||||
hidden_env = JS_Stone(js, hidden_env);
|
||||
|
||||
// Run through MACH VM
|
||||
crt->state = ACTOR_RUNNING;
|
||||
JSValue v = JS_Eval(js, data, engine_size, ENGINE, 0);
|
||||
free(data);
|
||||
JSValue v = JS_RunMachBin(js, (const uint8_t *)boot_data, boot_size, hidden_env);
|
||||
free(boot_data);
|
||||
uncaught_exception(js, v);
|
||||
crt->state = ACTOR_IDLE;
|
||||
set_actor_state(crt);
|
||||
@@ -213,15 +252,13 @@ static int run_test_suite(size_t heap_size)
|
||||
return 1;
|
||||
}
|
||||
|
||||
JSContext *ctx = JS_NewContextRawWithHeapSize(rt, heap_size);
|
||||
JSContext *ctx = JS_NewContextWithHeapSize(rt, heap_size);
|
||||
if (!ctx) {
|
||||
printf("Failed to create JS context\n");
|
||||
JS_FreeRuntime(rt);
|
||||
return 1;
|
||||
}
|
||||
|
||||
JS_AddIntrinsicBaseObjects(ctx);
|
||||
|
||||
int result = run_c_test_suite(ctx);
|
||||
|
||||
JS_FreeContext(ctx);
|
||||
@@ -230,122 +267,32 @@ static int run_test_suite(size_t heap_size)
|
||||
return result;
|
||||
}
|
||||
|
||||
/* Run an immediate script string */
|
||||
static int run_eval(const char *script_or_file, int print_bytecode)
|
||||
static void print_usage(const char *prog)
|
||||
{
|
||||
if (!find_cell_shop()) return 1;
|
||||
|
||||
/* Check if argument is a file path */
|
||||
struct stat st;
|
||||
char *script = NULL;
|
||||
char *allocated_script = NULL;
|
||||
const char *filename = "<eval>";
|
||||
|
||||
if (stat(script_or_file, &st) == 0 && S_ISREG(st.st_mode)) {
|
||||
/* It's a file, read its contents */
|
||||
FILE *f = fopen(script_or_file, "r");
|
||||
if (!f) {
|
||||
printf("Failed to open file: %s\n", script_or_file);
|
||||
return 1;
|
||||
}
|
||||
allocated_script = malloc(st.st_size + 1);
|
||||
if (!allocated_script) {
|
||||
fclose(f);
|
||||
printf("Failed to allocate memory for script\n");
|
||||
return 1;
|
||||
}
|
||||
size_t read_size = fread(allocated_script, 1, st.st_size, f);
|
||||
fclose(f);
|
||||
allocated_script[read_size] = '\0';
|
||||
script = allocated_script;
|
||||
filename = script_or_file;
|
||||
} else {
|
||||
/* Treat as inline script */
|
||||
script = (char *)script_or_file;
|
||||
}
|
||||
|
||||
JSRuntime *rt = JS_NewRuntime();
|
||||
if (!rt) {
|
||||
printf("Failed to create JS runtime\n");
|
||||
free(allocated_script);
|
||||
return 1;
|
||||
}
|
||||
|
||||
JSContext *ctx = JS_NewContextRaw(rt);
|
||||
if (!ctx) {
|
||||
printf("Failed to create JS context\n");
|
||||
JS_FreeRuntime(rt);
|
||||
free(allocated_script);
|
||||
return 1;
|
||||
}
|
||||
|
||||
JS_AddIntrinsicBaseObjects(ctx);
|
||||
JS_AddIntrinsicEval(ctx);
|
||||
JS_AddIntrinsicRegExp(ctx);
|
||||
JS_AddIntrinsicJSON(ctx);
|
||||
|
||||
int result = 0;
|
||||
|
||||
if (print_bytecode) {
|
||||
/* Compile only, then dump and optionally execute */
|
||||
JSValue func = JS_Eval(ctx, script, strlen(script), filename, JS_EVAL_FLAG_COMPILE_ONLY);
|
||||
if (JS_IsException(func)) {
|
||||
uncaught_exception(ctx, func);
|
||||
result = 1;
|
||||
} else {
|
||||
printf("=== Compiled Bytecode ===\n");
|
||||
JS_DumpFunctionBytecode(ctx, func);
|
||||
|
||||
/* Link - resolve global references */
|
||||
JSValue linked = JS_LinkFunction(ctx, func);
|
||||
if (JS_IsException(linked)) {
|
||||
uncaught_exception(ctx, linked);
|
||||
result = 1;
|
||||
} else {
|
||||
printf("\n=== Linked Bytecode ===\n");
|
||||
JS_DumpFunctionBytecode(ctx, linked);
|
||||
|
||||
/* Now execute the linked bytecode */
|
||||
JSValue v = JS_EvalFunction(ctx, linked);
|
||||
if (JS_IsException(v)) {
|
||||
uncaught_exception(ctx, v);
|
||||
result = 1;
|
||||
} else {
|
||||
JS_FreeValue(ctx, v);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
/* Compile, link, execute */
|
||||
JSValue func = JS_Eval(ctx, script, strlen(script), filename, JS_EVAL_FLAG_COMPILE_ONLY);
|
||||
if (JS_IsException(func)) {
|
||||
uncaught_exception(ctx, func);
|
||||
result = 1;
|
||||
} else {
|
||||
JSValue linked = JS_LinkFunction(ctx, func);
|
||||
if (JS_IsException(linked)) {
|
||||
uncaught_exception(ctx, linked);
|
||||
result = 1;
|
||||
} else {
|
||||
JSValue v = JS_EvalFunction(ctx, linked);
|
||||
if (JS_IsException(v)) {
|
||||
uncaught_exception(ctx, v);
|
||||
result = 1;
|
||||
} else {
|
||||
JS_FreeValue(ctx, v);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
JS_FreeContext(ctx);
|
||||
JS_FreeRuntime(rt);
|
||||
free(allocated_script);
|
||||
return result;
|
||||
printf("Usage: %s [options] <script> [args...]\n\n", prog);
|
||||
printf("Run a cell script (.ce actor or .cm module).\n\n");
|
||||
printf("Options:\n");
|
||||
printf(" --core <path> Set core path directly (overrides CELL_CORE)\n");
|
||||
printf(" --shop <path> Set shop path (overrides CELL_SHOP)\n");
|
||||
printf(" --emit-qbe Emit QBE IL (for native compilation)\n");
|
||||
printf(" --dump-mach Dump MACH bytecode disassembly\n");
|
||||
printf(" --test [heap_size] Run C test suite\n");
|
||||
printf(" -h, --help Show this help message\n");
|
||||
printf("\nEnvironment:\n");
|
||||
printf(" CELL_CORE Core path (default: <shop>/packages/core)\n");
|
||||
printf(" CELL_SHOP Shop path (default: ~/.cell)\n");
|
||||
printf("\nRecompile after changes: make\n");
|
||||
printf("Bootstrap from scratch: make bootstrap\n");
|
||||
}
|
||||
|
||||
int cell_init(int argc, char **argv)
|
||||
{
|
||||
/* Check for --help flag */
|
||||
if (argc >= 2 && (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0)) {
|
||||
print_usage(argv[0]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Check for --test flag to run C test suite */
|
||||
if (argc >= 2 && strcmp(argv[1], "--test") == 0) {
|
||||
size_t heap_size = 64 * 1024; /* 64KB default */
|
||||
@@ -359,50 +306,160 @@ int cell_init(int argc, char **argv)
|
||||
return run_test_suite(heap_size);
|
||||
}
|
||||
|
||||
/* Check for -e or --eval flag to run immediate script */
|
||||
/* Also check for -p flag to print bytecode */
|
||||
if (argc >= 3 && (strcmp(argv[1], "-e") == 0 || strcmp(argv[1], "--eval") == 0)) {
|
||||
return run_eval(argv[2], 0);
|
||||
}
|
||||
if (argc >= 3 && (strcmp(argv[1], "-p") == 0 || strcmp(argv[1], "--print-bytecode") == 0)) {
|
||||
return run_eval(argv[2], 1);
|
||||
/* Default: run script through bootstrap pipeline */
|
||||
int emit_qbe = 0;
|
||||
int dump_mach = 0;
|
||||
int arg_start = 1;
|
||||
const char *shop_override = NULL;
|
||||
const char *core_override = NULL;
|
||||
|
||||
// Parse flags (order-independent)
|
||||
while (arg_start < argc && argv[arg_start][0] == '-') {
|
||||
if (strcmp(argv[arg_start], "--mcode") == 0) {
|
||||
/* --mcode is now always on; accept and ignore for compat */
|
||||
arg_start++;
|
||||
} else if (strcmp(argv[arg_start], "--emit-qbe") == 0) {
|
||||
emit_qbe = 1;
|
||||
arg_start++;
|
||||
} else if (strcmp(argv[arg_start], "--dump-mach") == 0) {
|
||||
dump_mach = 1;
|
||||
arg_start++;
|
||||
} else if (strcmp(argv[arg_start], "--shop") == 0) {
|
||||
if (arg_start + 1 >= argc) {
|
||||
printf("ERROR: --shop requires a path argument\n");
|
||||
return 1;
|
||||
}
|
||||
shop_override = argv[arg_start + 1];
|
||||
arg_start += 2;
|
||||
} else if (strcmp(argv[arg_start], "--core") == 0) {
|
||||
if (arg_start + 1 >= argc) {
|
||||
printf("ERROR: --core requires a path argument\n");
|
||||
return 1;
|
||||
}
|
||||
core_override = argv[arg_start + 1];
|
||||
arg_start += 2;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int script_start = 1;
|
||||
if (!find_cell_shop(shop_override, core_override)) return 1;
|
||||
|
||||
/* Find the cell shop at ~/.cell */
|
||||
int found = find_cell_shop();
|
||||
if (!found) {
|
||||
actor_initialize();
|
||||
|
||||
size_t boot_size;
|
||||
char *boot_data = load_core_file(BOOTSTRAP_MACH, &boot_size);
|
||||
if (!boot_data) {
|
||||
printf("ERROR: Could not load bootstrap from %s\n", core_path);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Create the initial actor from the command line */
|
||||
int actor_argc = argc - script_start;
|
||||
char **actor_argv = argv + script_start;
|
||||
g_runtime = JS_NewRuntime();
|
||||
if (!g_runtime) {
|
||||
printf("Failed to create JS runtime\n");
|
||||
free(boot_data);
|
||||
return 1;
|
||||
}
|
||||
JSContext *ctx = JS_NewContextWithHeapSize(g_runtime, 16 * 1024 * 1024);
|
||||
if (!ctx) {
|
||||
printf("Failed to create JS context\n");
|
||||
free(boot_data); JS_FreeRuntime(g_runtime);
|
||||
return 1;
|
||||
}
|
||||
|
||||
WotaBuffer startwota;
|
||||
wota_buffer_init(&startwota, 5);
|
||||
wota_write_record(&startwota, 2);
|
||||
wota_write_text(&startwota, "program");
|
||||
wota_write_text(&startwota, actor_argv[0]);
|
||||
wota_write_text(&startwota, "arg");
|
||||
wota_write_array(&startwota, actor_argc - 1);
|
||||
for (int i = 1; i < actor_argc; i++)
|
||||
wota_write_text(&startwota, actor_argv[i]);
|
||||
|
||||
/* Initialize synchronization primitives */
|
||||
actor_initialize();
|
||||
|
||||
root_cell = create_actor(startwota.data);
|
||||
#ifndef TARGET_PLAYDATE
|
||||
signal(SIGINT, signal_handler);
|
||||
signal(SIGTERM, signal_handler);
|
||||
signal(SIGSEGV, signal_handler);
|
||||
signal(SIGABRT, signal_handler);
|
||||
#endif
|
||||
actor_loop();
|
||||
/* Create a cell_rt for the CLI context so JS-C bridge functions work */
|
||||
cell_rt *cli_rt = calloc(sizeof(*cli_rt), 1);
|
||||
cli_rt->mutex = malloc(sizeof(pthread_mutex_t));
|
||||
pthread_mutexattr_t mattr;
|
||||
pthread_mutexattr_init(&mattr);
|
||||
pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_RECURSIVE);
|
||||
pthread_mutex_init(cli_rt->mutex, &mattr);
|
||||
cli_rt->msg_mutex = malloc(sizeof(pthread_mutex_t));
|
||||
pthread_mutex_init(cli_rt->msg_mutex, &mattr);
|
||||
pthread_mutexattr_destroy(&mattr);
|
||||
|
||||
return 0;
|
||||
cli_rt->context = ctx;
|
||||
JS_SetContextOpaque(ctx, cli_rt);
|
||||
JS_SetInterruptHandler(ctx, (JSInterruptHandler *)actor_interrupt_cb, cli_rt);
|
||||
|
||||
JS_AddGCRef(ctx, &cli_rt->idx_buffer_ref);
|
||||
JS_AddGCRef(ctx, &cli_rt->on_exception_ref);
|
||||
JS_AddGCRef(ctx, &cli_rt->message_handle_ref);
|
||||
JS_AddGCRef(ctx, &cli_rt->unneeded_ref);
|
||||
JS_AddGCRef(ctx, &cli_rt->actor_sym_ref);
|
||||
cli_rt->idx_buffer_ref.val = JS_NULL;
|
||||
cli_rt->on_exception_ref.val = JS_NULL;
|
||||
cli_rt->message_handle_ref.val = JS_NULL;
|
||||
cli_rt->unneeded_ref.val = JS_NULL;
|
||||
cli_rt->actor_sym_ref.val = JS_NewObject(ctx);
|
||||
|
||||
root_cell = cli_rt;
|
||||
|
||||
JS_FreeValue(ctx, js_blob_use(ctx));
|
||||
|
||||
JSValue hidden_env = JS_NewObject(ctx);
|
||||
JS_SetPropertyStr(ctx, hidden_env, "os", js_os_use(ctx));
|
||||
JS_SetPropertyStr(ctx, hidden_env, "core_path", JS_NewString(ctx, core_path));
|
||||
JS_SetPropertyStr(ctx, hidden_env, "shop_path",
|
||||
shop_path ? JS_NewString(ctx, shop_path) : JS_NULL);
|
||||
JS_SetPropertyStr(ctx, hidden_env, "emit_qbe", JS_NewBool(ctx, emit_qbe));
|
||||
JS_SetPropertyStr(ctx, hidden_env, "dump_mach", JS_NewBool(ctx, dump_mach));
|
||||
JS_SetPropertyStr(ctx, hidden_env, "actorsym", JS_DupValue(ctx, cli_rt->actor_sym_ref.val));
|
||||
JS_SetPropertyStr(ctx, hidden_env, "json", js_json_use(ctx));
|
||||
JS_SetPropertyStr(ctx, hidden_env, "nota", js_nota_use(ctx));
|
||||
JS_SetPropertyStr(ctx, hidden_env, "wota", js_wota_use(ctx));
|
||||
JS_SetPropertyStr(ctx, hidden_env, "init", JS_NULL);
|
||||
JSValue args_arr = JS_NewArray(ctx);
|
||||
for (int i = arg_start; i < argc; i++) {
|
||||
JSValue str = JS_NewString(ctx, argv[i]);
|
||||
JS_ArrayPush(ctx, &args_arr, str);
|
||||
}
|
||||
JS_SetPropertyStr(ctx, hidden_env, "args", args_arr);
|
||||
hidden_env = JS_Stone(ctx, hidden_env);
|
||||
|
||||
JSValue result = JS_RunMachBin(ctx, (const uint8_t *)boot_data, boot_size, hidden_env);
|
||||
free(boot_data);
|
||||
|
||||
int exit_code = 0;
|
||||
if (JS_IsException(result)) {
|
||||
JS_GetException(ctx);
|
||||
exit_code = 1;
|
||||
} else if (!JS_IsNull(result)) {
|
||||
const char *str = JS_ToCString(ctx, result);
|
||||
if (str) {
|
||||
printf("%s\n", str);
|
||||
JS_FreeCString(ctx, str);
|
||||
}
|
||||
}
|
||||
|
||||
if (scheduler_actor_count() > 0) {
|
||||
scheduler_enable_quiescence();
|
||||
actor_loop();
|
||||
exit_handler();
|
||||
exit(0);
|
||||
}
|
||||
|
||||
/* No actors spawned — clean up CLI context */
|
||||
JS_DeleteGCRef(ctx, &cli_rt->idx_buffer_ref);
|
||||
JS_DeleteGCRef(ctx, &cli_rt->on_exception_ref);
|
||||
JS_DeleteGCRef(ctx, &cli_rt->message_handle_ref);
|
||||
JS_DeleteGCRef(ctx, &cli_rt->unneeded_ref);
|
||||
JS_DeleteGCRef(ctx, &cli_rt->actor_sym_ref);
|
||||
JS_SetInterruptHandler(ctx, NULL, NULL);
|
||||
|
||||
pthread_mutex_destroy(cli_rt->mutex);
|
||||
free(cli_rt->mutex);
|
||||
pthread_mutex_destroy(cli_rt->msg_mutex);
|
||||
free(cli_rt->msg_mutex);
|
||||
free(cli_rt);
|
||||
root_cell = NULL;
|
||||
|
||||
JS_FreeContext(ctx);
|
||||
JS_FreeRuntime(g_runtime);
|
||||
g_runtime = NULL;
|
||||
|
||||
exit_handler();
|
||||
return exit_code;
|
||||
}
|
||||
|
||||
int JS_ArrayLength(JSContext *js, JSValue a)
|
||||
@@ -436,36 +493,16 @@ double cell_random() {
|
||||
|
||||
void cell_trace_sethook(cell_hook)
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
int uncaught_exception(JSContext *js, JSValue v)
|
||||
{
|
||||
cell_rt *rt = JS_GetContextOpaque(js);
|
||||
if (!JS_HasException(js)) {
|
||||
JS_FreeValue(js,v);
|
||||
(void)v;
|
||||
if (!JS_HasException(js))
|
||||
return 1;
|
||||
}
|
||||
|
||||
JSValue exp = JS_GetException(js);
|
||||
|
||||
JSValue message = JS_GetPropertyStr(js, exp, "message");
|
||||
const char *msg_str = JS_ToCString(js, message);
|
||||
if (msg_str) {
|
||||
printf("Exception: %s\n", msg_str);
|
||||
JS_FreeCString(js, msg_str);
|
||||
}
|
||||
JS_FreeValue(js, message);
|
||||
|
||||
JSValue stack = JS_GetPropertyStr(js, exp, "stack");
|
||||
const char *stack_str = JS_ToCString(js, stack);
|
||||
if (stack_str) {
|
||||
printf("Stack:\n%s\n", stack_str);
|
||||
JS_FreeCString(js, stack_str);
|
||||
}
|
||||
JS_FreeValue(js, stack);
|
||||
|
||||
JS_FreeValue(js, exp);
|
||||
JS_FreeValue(js, v);
|
||||
/* Error message and backtrace were already printed to stderr
|
||||
by JS_ThrowError2 / print_backtrace. Just clear the flag. */
|
||||
JS_GetException(js);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user