Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Don Isaac committed Dec 13, 2024
1 parent 3b7a89d commit aa5245f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
42 changes: 33 additions & 9 deletions src/bun.js/node/node_assert_binding.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const std = @import("std");
const bun = @import("root").bun;
const JSC = bun.JSC;
const assert = @import("./node_assert.zig");
const Diff = assert.Diff;
const Allocator = std.mem.Allocator;

/// ```ts
Expand All @@ -23,25 +24,48 @@ pub fn myersDiff(global: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSE
defer arena.deinit();
const alloc = arena.allocator();

const actual = try safeToString(global, alloc, callframe.argument(0));
const expected = try safeToString(global, alloc, callframe.argument(1));
const actual = try safeToString(global, alloc, callframe.argument(0), "actual");
const expected = try safeToString(global, alloc, callframe.argument(1), "expected");

const diff = assert.myersDiff(arena.allocator(), actual.slice(), expected.slice()) catch |e| {
return switch (e) {
error.OutOfMemory => return global.throwOutOfMemory(),
};
};

return JSC.toJS(global, []const assert.Diff, diff.items, .allocated);
// todo: replace with toJS
var array = JSC.JSValue.createEmptyArray(global, diff.items.len);
for (diff.items, 0..) |*line, i| {
var obj = JSC.JSValue.createEmptyObjectWithNullPrototype(global);
if (obj == .zero) return global.throwOutOfMemory();
obj.put(global, bun.String.static("operation"), JSC.JSValue.jsNumber(@as(u32, @intFromEnum(line.operation))));
obj.put(global, bun.String.static("text"), JSC.toJS(global, []const u8, line.text, .allocated));
array.putIndex(global, @truncate(i), obj);
}
return array;
}

fn safeToString(global: *JSC.JSGlobalObject, alloc: Allocator, argument: JSC.JSValue) bun.JSError!bun.JSC.ZigString.Slice {
if (!argument.isString()) {
return global.throwInvalidArgumentTypeValue("argument", "string", argument);
fn safeToString(global: *JSC.JSGlobalObject, arena: Allocator, argument: JSC.JSValue, comptime argname: []const u8) bun.JSError!bun.JSC.ZigString.Slice {
if (argument.isString()) {
const bunstring = argument.toBunString2(global) catch @panic("argument is string-like but could not be converted into a bun.String. This is a bug.");
return bunstring.toUTF8WithoutRef(arena);
} else if (argument.isObject()) {
const to_string: JSC.JSValue = (try argument.getFunction(global, "toString")) orelse {
return global.throwInvalidArgumentTypeValue(argname, "string or object with .toString()", argument);
};
const js_string = try to_string.call(
global,
argument,
&[0]JSC.JSValue{},
);
const bun_string = bun.String.fromJS(js_string, global);
// TODO: does bunstring own its memory or does it increment a reference
// count? IF the former, it's saved in the arena and this won't leak,
// otherwise this will cause a UAF.
return bun_string.toUTF8WithoutRef(arena);
} else {
return global.throwInvalidArgumentTypeValue(argname, "string or object with .toString()", argument);
}

const bunstring = argument.toBunString2(global) catch @panic("argument is string-like but could not be converted into a bun.String. This is a bug.");
return bunstring.toUTF8WithoutRef(alloc);
}

pub fn generate(global: *JSC.JSGlobalObject) JSC.JSValue {
Expand Down
3 changes: 2 additions & 1 deletion src/js/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"esModuleInterop": true,
// Path remapping
"baseUrl": ".",
"paths": {
"internal/*": ["./js/internal/*"] //deprecated
"internal/*": ["./internal/*"] //deprecated
}
},
"include": ["**/*.ts", "**/*.tsx", "./builtins.d.ts", "./private.d.ts"]
Expand Down

0 comments on commit aa5245f

Please sign in to comment.