Skip to content

Commit

Permalink
Merge pull request #141 from Browsercore/uppercase_jsname
Browse files Browse the repository at this point in the history
Handle uppercase js names
  • Loading branch information
francisbouvier authored Nov 24, 2023
2 parents 2ac2522 + c190af7 commit 4b0aee3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
31 changes: 21 additions & 10 deletions src/reflect.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1234,18 +1234,29 @@ pub fn do(comptime types: anytype) Error![]Struct {
// Utils funcs
// -----------

fn jsName(comptime name: []const u8) []u8 {
comptime {
var js_name: [name.len]u8 = undefined;
js_name[0] = std.ascii.toLower(name[0]);
for (name, 0..) |char, i| {
if (i == 0) {
continue;
}
js_name[i] = char;
fn jsName(comptime name: []const u8) []const u8 {
std.debug.assert(@inComptime());

// uppercase names should not change
var is_upper = true;
for (name) |char| {
if (!std.ascii.isUpper(char)) {
is_upper = false;
break;
}
}
if (is_upper) return name;

// otherwhise lower first character
var js_name: [name.len]u8 = undefined;
js_name[0] = std.ascii.toLower(name[0]);
for (name, 0..) |char, i| {
if (i == 0) {
continue;
}
return &js_name;
js_name[i] = char;
}
return &js_name;
}

fn shortName(comptime T: type) []const u8 {
Expand Down
10 changes: 10 additions & 0 deletions src/tests/proto_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ const Person = struct {
return true;
}

pub fn get_UPPER(_: Person) bool {
return true;
}

pub fn _UPPERMETHOD(_: Person) bool {
return true;
}

pub fn set_allocator(_: *Person, alloc: std.mem.Allocator, _: bool) void {
Person.allocTest(alloc) catch unreachable;
}
Expand Down Expand Up @@ -239,6 +247,8 @@ pub fn exec(
var cases2 = [_]tests.Case{
.{ .src = "p.age === 40", .ex = "true" },
.{ .src = "p.allocator", .ex = "true" },
.{ .src = "p.UPPER", .ex = "true" },
.{ .src = "p.UPPERMETHOD()", .ex = "true" },
};
try tests.checkCases(js_env, &cases2);

Expand Down

0 comments on commit 4b0aee3

Please sign in to comment.