-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroot.zig
More file actions
102 lines (93 loc) · 4.11 KB
/
Copy pathroot.zig
File metadata and controls
102 lines (93 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//! zig-js — a homegrown JavaScript engine in pure Zig.
//!
//! Two ways to use it:
//! 1. As a Zig module (`@import("js")`): drive the engine directly via
//! `Context.create` / `Context.evaluate`, working with `Value`.
//! 2. Through the implemented JavaScriptCore-shaped C API subset: link the
//! static library and call the exported `JSGlobalContextCreate` /
//! `JSEvaluateScript` / ... symbols. See `c_api.zig`.
//!
//! v1 scope: expressions, `var`/`let`/`const`, `if`/`else`, `while`, blocks,
//! string concatenation, and the JSC value/string/object C-API surface that
//! `~/Code/Home/lang`'s runtime consumes. Functions, closures, and a real GC
//! are the next milestones (see craft's docs/architecture/web-engine-plan.md).
const std = @import("std");
// Engine core
pub const Value = @import("value.zig").Value;
pub const Object = @import("value.zig").Object;
pub const NativeFn = @import("value.zig").NativeFn;
pub const HostError = @import("value.zig").HostError;
pub const strictEquals = @import("value.zig").strictEquals;
pub const looseEquals = @import("value.zig").looseEquals;
pub const Context = @import("context.zig").Context;
pub const Interpreter = @import("interpreter.zig").Interpreter;
pub const Environment = @import("interpreter.zig").Environment;
pub const Lexer = @import("lexer.zig").Lexer;
pub const Parser = @import("parser.zig").Parser;
pub const JsString = @import("jsstring.zig").JsString;
pub const installGlobals = @import("interpreter.zig").installGlobals;
pub const shape = @import("shape.zig");
pub const promise_profile = @import("promise_profile.zig");
// Agent/threading infrastructure ($262.agent, Atomics waiter table). Conformance
// runners use `Context.TestingOptions.main_can_block` to model [[CanBlock]].
pub const agent = @import("agent.zig");
// Worker agents: one Context per OS thread, postMessage over the
// structured-clone wire format, cooperative terminate.
pub const Worker = @import("worker.zig").Worker;
// Shared-realm Thread API internals.
pub const jsthread = @import("jsthread.zig");
// Bytecode pipeline (tier-1 VM): compiler lowers the AST, vm executes it.
pub const bytecode = @import("bytecode.zig");
pub const Compiler = @import("compiler.zig").Compiler;
pub const vm = @import("vm.zig");
pub const jit = @import("jit.zig");
// JavaScriptCore-shaped C API subset (re-exported for documentation / direct use).
pub const c_api = @import("c_api.zig");
// The precise-GC binding (issue #1 Phase 7). Opt-in contexts allocate heap
// cells through it; see docs/threads/P7-gc-design.md.
pub const gc = @import("gc.zig");
/// Convenience: evaluate a snippet in a throwaway context, returning the
/// completion value. The caller owns nothing — the context is destroyed, so
/// only copy out primitives (numbers/booleans). For strings/objects, drive a
/// `Context` directly.
pub fn evalNumber(source: []const u8) !f64 {
const ctx = try Context.create(std.heap.page_allocator);
defer ctx.destroy();
const v = try ctx.evaluate(source);
return v.toNumber();
}
test {
// Pull in every module so `zig build test` runs their inline tests.
_ = @import("value.zig");
_ = @import("shape.zig");
_ = @import("shared_buffer.zig");
_ = @import("agent.zig");
_ = @import("structured_clone.zig");
_ = @import("worker.zig");
_ = @import("gil.zig");
_ = @import("jsthread.zig");
_ = @import("promise_profile.zig");
_ = @import("jsstring.zig");
_ = @import("lexer.zig");
_ = @import("ast.zig");
_ = @import("parser.zig");
_ = @import("interpreter.zig");
_ = @import("builtins.zig");
_ = @import("unicode_normalize.zig");
_ = @import("bytecode.zig");
_ = @import("compiler.zig");
_ = @import("vm.zig");
_ = @import("jit.zig");
_ = @import("context.zig");
_ = @import("c_api.zig");
_ = @import("gc.zig");
_ = @import("nanbox.zig");
_ = @import("strcell.zig");
_ = @import("valuebox.zig");
_ = @import("value_nb.zig");
_ = @import("root_handshake.zig");
_ = @import("parallel_lock.zig");
}
test "evalNumber convenience" {
try std.testing.expectEqual(@as(f64, 6), try evalNumber("2 * 3"));
}