Skip to content

Commit 37dad0c

Browse files
committed
add access to global context to scripty builtins
1 parent 7bd7460 commit 37dad0c

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

src/fuzz/context.zig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,15 @@ const TestValue = union(Tag) {
5252
}
5353
}
5454

55-
pub const call = scripty.defaultCall(TestValue);
55+
pub const call = scripty.defaultCall(TestValue, TestContext);
5656

5757
pub fn builtinsFor(comptime tag: Tag) type {
5858
const StringBuiltins = struct {
5959
pub const len = struct {
6060
pub fn call(
6161
str: []const u8,
6262
gpa: std.mem.Allocator,
63+
_: *const TestContext,
6364
args: []const TestValue,
6465
) !TestValue {
6566
if (args.len != 0) return .{ .err = "'len' wants no arguments" };

src/types.zig

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,18 @@ pub fn defaultDot(
2929
}.dot;
3030
}
3131

32-
pub fn defaultCall(comptime Value: type) fn (
32+
pub fn defaultCall(Value: type, Context: type) fn (
3333
Value,
3434
std.mem.Allocator,
35+
*const Context,
3536
[]const u8,
3637
[]const Value,
3738
) error{ OutOfMemory, Interrupt }!Value {
3839
return struct {
3940
pub fn call(
4041
value: Value,
4142
gpa: std.mem.Allocator,
43+
ctx: *const Context,
4244
fn_name: []const u8,
4345
args: []const Value,
4446
) error{ OutOfMemory, Interrupt }!Value {
@@ -55,6 +57,7 @@ pub fn defaultCall(comptime Value: type) fn (
5557
return @field(Builtin, decl.name).call(
5658
v,
5759
gpa,
60+
ctx,
5861
args,
5962
);
6063
}
@@ -63,6 +66,7 @@ pub fn defaultCall(comptime Value: type) fn (
6366
if (hasDecl(@TypeOf(v), "fallbackCall")) {
6467
return v.fallbackCall(
6568
gpa,
69+
ctx,
6670
fn_name,
6771
args,
6872
);

src/vm.zig

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ pub fn VM(
257257
stack_locs[call_idx].start = call_loc.start;
258258
stack_locs[call_idx].end = call_loc.end + 1;
259259

260-
const new_value = old_value.call(gpa, fn_name, args) catch |err| switch (err) {
260+
const new_value = old_value.call(gpa, ctx, fn_name, args) catch |err| switch (err) {
261261
error.OutOfMemory => return error.OutOfMemory,
262262
error.Interrupt => {
263263
vm.state = .waiting;
@@ -340,14 +340,15 @@ pub const TestValue = union(Tag) {
340340
}
341341
}
342342

343-
pub const call = types.defaultCall(TestValue);
343+
pub const call = types.defaultCall(TestValue, TestContext);
344344

345345
pub fn builtinsFor(comptime tag: Tag) type {
346346
const StringBuiltins = struct {
347347
pub const len = struct {
348348
pub fn call(
349349
str: []const u8,
350350
gpa: std.mem.Allocator,
351+
_: *const TestContext,
351352
args: []const TestValue,
352353
) !TestValue {
353354
if (args.len != 0) return .{
@@ -360,6 +361,7 @@ pub const TestValue = union(Tag) {
360361
pub fn call(
361362
str: []const u8,
362363
gpa: std.mem.Allocator,
364+
_: *const TestContext,
363365
args: []const TestValue,
364366
) !TestValue {
365367
if (args.len != 0) return .{

0 commit comments

Comments
 (0)