-
-
Save koljakube/0f4555cb69abeea90c56c4a18ff1db03 to your computer and use it in GitHub Desktop.
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
var test_class_was_allocated = false; | |
var test_class_was_allocated_with_3_params = false; | |
var test_class_was_called = false; | |
var test_class_was_finalized = false; | |
const TestClass = struct { | |
const Self = @This(); | |
// Make the struct have a size > 0. | |
data: [4]u8, | |
pub fn initialize0(self: *Self, vm: *Vm) !void { | |
test_class_was_allocated = true; | |
} | |
pub fn initialize3(self: *Self, vm: *Vm, x: i32, y: i32, z: i32) !void { | |
test_class_was_allocated_with_3_params = true; | |
} | |
pub fn finalize(self: *Self) void { | |
test_class_was_finalized = true; | |
} | |
pub fn call(self: *Self, vm: *Vm, str: []const u8) void { | |
if (std.mem.eql(u8, str, "lemon")) { | |
test_class_was_called = true; | |
} | |
} | |
}; | |
var adder_works = false; | |
const Adder = struct { | |
const Self = @This(); | |
summand: u32, | |
pub fn initialize1(self: *Self, vm: *Vm, summand: u32) void { | |
self.summand = summand; | |
} | |
pub fn add(self: *Self, vm: *Vm, summand: u32) void { | |
if (self.summand == 5 and summand == 3) { | |
adder_works = true; | |
} | |
} | |
}; | |
test "accessing foreign classes" { | |
const allocator = std.testing.allocator; | |
var config = Configuration{}; | |
config.errorFn = vmPrintError; | |
config.writeFn = vmPrint; | |
registerForeignClasses(&config, .{ | |
ForeignClass("TestClass", TestClass), | |
ForeignClass("Adder", Adder), | |
}); | |
var vm: Vm = undefined; | |
try config.newVmInPlace(EmptyUserData, &vm, null); | |
try vm.interpret("test", | |
\\foreign class TestClass { | |
\\ construct new() {} | |
\\ construct new(a, b, c) {} | |
\\ foreign call(s) | |
\\} | |
\\var tc = TestClass.new() | |
\\tc.call("lemon") | |
\\ | |
\\var tc2 = TestClass.new(1, 2, 3) | |
\\ | |
\\foreign class Adder { | |
\\ construct new(summand) {} | |
\\ foreign add(summand) | |
\\} | |
\\var adder = Adder.new(5) | |
\\adder.add(3) | |
); | |
vm.deinit(); | |
testing.expect(test_class_was_allocated); | |
testing.expect(test_class_was_finalized); | |
testing.expect(test_class_was_called); | |
testing.expect(test_class_was_allocated_with_3_params); | |
testing.expect(adder_works); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment