diff --git a/src/lib/buzz_std.zig b/src/lib/buzz_std.zig index 946952b1..d7c9ef3d 100644 --- a/src/lib/buzz_std.zig +++ b/src/lib/buzz_std.zig @@ -102,7 +102,7 @@ export fn parseInt(ctx: *api.NativeCtx) c_int { const string_slice = string.?[0..len]; - const number: i32 = std.fmt.parseInt(i32, string_slice, 10) catch { + const number = std.fmt.parseInt(i32, string_slice, 10) catch { ctx.vm.bz_push(api.Value.Null); return 1; @@ -113,6 +113,35 @@ export fn parseInt(ctx: *api.NativeCtx) c_int { return 1; } +export fn parseUd(ctx: *api.NativeCtx) c_int { + const string_value = ctx.vm.bz_peek(0); + + var len: usize = 0; + const string = string_value.bz_valueToString(&len); + + if (len == 0) { + ctx.vm.bz_push(api.Value.Null); + + return 1; + } + + const string_slice = string.?[0..len]; + + const number = std.fmt.parseInt(u64, string_slice, 10) catch { + ctx.vm.bz_push(api.Value.Null); + + return 1; + }; + + if (api.ObjUserData.bz_newUserData(ctx.vm, number)) |userdata| { + ctx.vm.bz_pushUserData(userdata); + + return 1; + } else { + @panic("Out of memory"); + } +} + export fn parseFloat(ctx: *api.NativeCtx) c_int { const string_value = ctx.vm.bz_peek(0); diff --git a/src/lib/std.buzz b/src/lib/std.buzz index 601a7fcd..3ed90625 100644 --- a/src/lib/std.buzz +++ b/src/lib/std.buzz @@ -7,12 +7,12 @@ export extern fun assert(bool condition, str? message = null) > void; || @param value value to print export extern fun print(str value) > void; -|| Parse number, returns false if string does not represent a number +|| Parse integer, returns false if string does not represent a integer || @param string string to parse || @return integer parsed or null export extern fun parseInt(str string) > int?; -|| Parse number, returns false if string does not represent a number +|| Parse float, returns false if string does not represent a float || @param string string to parse || @return float parsed or null export extern fun parseFloat(str string) > float?; @@ -33,6 +33,11 @@ export extern fun toFloat(int n) > float; || @return casted value or 0 if value provided is not a number export extern fun toUd(any n) > ud; +|| Parse ud, returns false if string does not represent a ud (u64) +|| @param string string to parse +|| @return ud parsed or null +export extern fun parseUd(str string) > ud?; + || Return ascii char for given byte export extern fun char(int byte) > str; diff --git a/src/obj.zig b/src/obj.zig index 418c34e5..f628ef84 100644 --- a/src/obj.zig +++ b/src/obj.zig @@ -451,6 +451,12 @@ pub const Obj = struct { return self_enum_instance.enum_ref == other_enum_instance.enum_ref and self_enum_instance.case == other_enum_instance.case; }, + .UserData => { + const self_userdata: *ObjUserData = ObjUserData.cast(self).?; + const other_userdata: *ObjUserData = ObjUserData.cast(other).?; + + return self_userdata.userdata == other_userdata.userdata; + }, .Bound, .Closure, .Function, @@ -460,7 +466,6 @@ pub const Obj = struct { .Map, .Enum, .Native, - .UserData, .Fiber, .ForeignStruct, => { diff --git a/tests/023-std.buzz b/tests/023-std.buzz index 84af23b1..e6e10489 100644 --- a/tests/023-std.buzz +++ b/tests/023-std.buzz @@ -9,6 +9,8 @@ test "parseInt/Float" { assert(toFloat(23) == 23.0, message: "Could cast int to float"); assert(toUd(23) is ud, message: "Could cast int to ud"); assert(toUd(23.0) is ud, message: "Could cast float to ud"); + + assert(parseUd("42") == toUd(42), message: "Could parse ud"); } test "char" {