-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #220 from lightpanda-io/userctx
Implement a UserContext
- Loading branch information
Showing
13 changed files
with
139 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
const std = @import("std"); | ||
|
||
const public = @import("../api.zig"); | ||
const tests = public.test_utils; | ||
|
||
const Config = struct { | ||
use_proxy: bool, | ||
}; | ||
|
||
pub const UserContext = Config; | ||
|
||
const Request = struct { | ||
use_proxy: bool, | ||
|
||
pub fn constructor(ctx: Config) Request { | ||
return .{ | ||
.use_proxy = ctx.use_proxy, | ||
}; | ||
} | ||
|
||
pub fn get_proxy(self: *Request) bool { | ||
return self.use_proxy; | ||
} | ||
|
||
pub fn _configProxy(_: *Request, ctx: Config) bool { | ||
return ctx.use_proxy; | ||
} | ||
}; | ||
|
||
pub const Types = .{ | ||
Request, | ||
}; | ||
|
||
// exec tests | ||
pub fn exec( | ||
alloc: std.mem.Allocator, | ||
js_env: *public.Env, | ||
) anyerror!void { | ||
try js_env.setUserContext(Config{ | ||
.use_proxy = true, | ||
}); | ||
|
||
// start JS env | ||
try js_env.start(alloc); | ||
defer js_env.stop(); | ||
|
||
var tc = [_]tests.Case{ | ||
.{ .src = "const req = new Request();", .ex = "undefined" }, | ||
.{ .src = "req.proxy", .ex = "true" }, | ||
.{ .src = "req.configProxy()", .ex = "true" }, | ||
}; | ||
try tests.checkCases(js_env, &tc); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const std = @import("std"); | ||
|
||
// UserContext is a type defined by the user optionally passed to the native | ||
// API. | ||
// The type is defined via a root declaration. | ||
// Request a UserContext parameter in your native implementation to get the | ||
// context. | ||
pub const UserContext = blk: { | ||
const root = @import("root"); | ||
if (@hasDecl(root, "UserContext")) { | ||
break :blk root.UserContext; | ||
} | ||
|
||
// when no declaration is given, UserContext is define with an empty struct. | ||
break :blk struct {}; | ||
}; |