Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix napi_get_value_string_utf8 to match node #7175

Merged
merged 5 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/bun.js/bindings/napi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1771,15 +1771,22 @@ extern "C" napi_status napi_get_value_string_utf8(napi_env env,
return napi_ok;
}

if (bufsize == NAPI_AUTO_LENGTH) {
bufsize = strlen(buf);
if (UNLIKELY(bufsize == 0)) {
*writtenPtr = 0;
return napi_ok;
}

if (UNLIKELY(bufsize == NAPI_AUTO_LENGTH)) {
*writtenPtr = 0;
buf[0] = '\0';
return napi_ok;
}

size_t written;
if (view.is8Bit()) {
written = Bun__encoding__writeLatin1(view.characters8(), view.length(), reinterpret_cast<unsigned char*>(buf), bufsize, static_cast<uint8_t>(WebCore::BufferEncodingType::utf8));
written = Bun__encoding__writeLatin1(view.characters8(), view.length(), reinterpret_cast<unsigned char*>(buf), bufsize - 1, static_cast<uint8_t>(WebCore::BufferEncodingType::utf8));
} else {
written = Bun__encoding__writeUTF16(view.characters16(), view.length(), reinterpret_cast<unsigned char*>(buf), bufsize, static_cast<uint8_t>(WebCore::BufferEncodingType::utf8));
written = Bun__encoding__writeUTF16(view.characters16(), view.length(), reinterpret_cast<unsigned char*>(buf), bufsize - 1, static_cast<uint8_t>(WebCore::BufferEncodingType::utf8));
}

if (writtenPtr != nullptr) {
Expand Down
3 changes: 3 additions & 0 deletions test/napi/napi-app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
*.log
build
18 changes: 18 additions & 0 deletions test/napi/napi-app/binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"targets": [{
"target_name": "napitests",
"cflags!": [ "-fno-exceptions" ],
"cflags_cc!": [ "-fno-exceptions" ],
"sources": [
"main.cpp"
],
'include_dirs': [
"<!@(node -p \"require('node-addon-api').include\")"
],
'libraries': [],
'dependencies': [
"<!(node -p \"require('node-addon-api').gyp\")"
],
'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ]
}]
}
Binary file added test/napi/napi-app/bun.lockb
Binary file not shown.
67 changes: 67 additions & 0 deletions test/napi/napi-app/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* cppsrc/main.cpp */
#include <napi.h>
#include <iostream>



napi_value fail(napi_env env, const char *msg)
{
napi_value result;
napi_create_string_utf8(env, msg, NAPI_AUTO_LENGTH, &result);
return result;
}

napi_value ok(napi_env env)
{
napi_value result;
napi_get_undefined(env, &result);
return result;
}


napi_value test_napi_get_value_string_utf8_with_buffer(const Napi::CallbackInfo &info)
{
Napi::Env env = info.Env();

// get how many chars we need to copy
uint32_t _len;
if (napi_get_value_uint32(env, info[1], &_len) != napi_ok) {
return fail(env, "call to napi_get_value_uint32 failed");
}
size_t len = (size_t)_len;

if (len == 424242) {
len = NAPI_AUTO_LENGTH;
} else if (len > 29) {
return fail(env, "len > 29");
}

size_t copied;
size_t BUF_SIZE = 30;
char buf[BUF_SIZE];
memset(buf, '*', BUF_SIZE);
buf[BUF_SIZE] = '\0';

if (napi_get_value_string_utf8(env, info[0], buf, len, &copied) != napi_ok) {
return fail(env, "call to napi_get_value_string_utf8 failed");
}

std::cout << "Chars to copy: " << len << std::endl;
std::cout << "Copied chars: " << copied << std::endl;
std::cout << "Buffer: ";
for (int i = 0; i < BUF_SIZE; i++) {
std::cout << (int)buf[i] << ", ";
}
std::cout << std::endl;
std::cout << "Value str: " << buf << std::endl;
return ok(env);
}

Napi::Object InitAll(Napi::Env env, Napi::Object exports)
{
exports.Set(
"test_napi_get_value_string_utf8_with_buffer", Napi::Function::New(env, test_napi_get_value_string_utf8_with_buffer));
return exports;
}

NODE_API_MODULE(napitests, InitAll)
9 changes: 9 additions & 0 deletions test/napi/napi-app/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const tests = require("./build/Release/napitests.node");
const fn = tests[process.argv[2]];
if (typeof fn !== "function") {
throw new Error("Unknown test:", process.argv[2]);
}
const result = fn.apply(null, JSON.parse(process.argv[3] ?? "[]"));
if (result) {
throw new Error(result);
}
13 changes: 13 additions & 0 deletions test/napi/napi-app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "napitests",
"version": "1.0.0",
"gypfile": true,
"scripts": {
"build": "node-gyp rebuild",
"clean": "node-gyp clean"
},
"devDependencies": {
"node-gyp": "^10.0.1",
"node-addon-api": "^7.0.0"
}
}
66 changes: 66 additions & 0 deletions test/napi/napi.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { it, expect, test, beforeAll, describe } from "bun:test";
oguimbal marked this conversation as resolved.
Show resolved Hide resolved
import { bunExe, bunEnv } from "harness";
import { spawnSync } from "bun";
import { join } from "path";

describe("napi", () => {
beforeAll(() => {
// build gyp
const build = spawnSync({
cmd: ["yarn", "build"],
cwd: join(__dirname, "napi-app"),
});
if (!build.success) {
console.error(build.stderr.toString());
throw new Error("build failed");
}
});

describe("napi_get_value_string_utf8 with buffer", () => {
// see https://github.com/oven-sh/bun/issues/6949
it("copies one char", () => {
const result = checkSameOutput("test_napi_get_value_string_utf8_with_buffer", ["abcdef", 2]);
expect(result).toEndWith("str: a");
});

it("copies null terminator", () => {
const result = checkSameOutput("test_napi_get_value_string_utf8_with_buffer", ["abcdef", 1]);
expect(result).toEndWith("str:");
});

it("copies zero char", () => {
const result = checkSameOutput("test_napi_get_value_string_utf8_with_buffer", ["abcdef", 0]);
expect(result).toEndWith("str: ******************************");
});

it("copies more than given len", () => {
const result = checkSameOutput("test_napi_get_value_string_utf8_with_buffer", ["abcdef", 25]);
expect(result).toEndWith("str: abcdef");
});

it("copies auto len", () => {
const result = checkSameOutput("test_napi_get_value_string_utf8_with_buffer", ["abcdef", 424242]);
expect(result).toEndWith("str:");
});
});
});

function checkSameOutput(test: string, args: any[]) {
const nodeResult = runOn("node", test, args).trim();
let bunResult = runOn(join(__dirname, "../../build/bun-debug"), test, args);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let bunResult = runOn(join(__dirname, "../../build/bun-debug"), test, args);
let bunResult = runOn(bunExe(), test, args);

Copy link
Contributor Author

@oguimbal oguimbal Nov 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I do this locally, tests fail again as if it was run on the current bun version... is bunExe() supposed to refer to the version of bun built by the pipeline ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm. yes, is bun-debug available in $PATH?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, but it's available as bug-debug, not bun. Thus, these tests only pass locally if I run them via bun run build && bun-debug test test/napi (with a lot of noisy logs, obviously).

If that's the executable that runs on your test pipeline instead of the current version of bun, then fine, lets commit that 🙃

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests pass locally with process.execPath in use and they will not pass in CI without that.

// remove all debug logs
bunResult = bunResult.replaceAll(/^\[\w+\].+$/gm, "").trim();
expect(bunResult).toBe(nodeResult);
return nodeResult;
}

function runOn(executable: string, test: string, args: any[]) {
const exec = spawnSync({
cmd: [executable, join(__dirname, "napi-app/main.js"), test, JSON.stringify(args)],
oguimbal marked this conversation as resolved.
Show resolved Hide resolved
env: bunEnv,
});
const errs = exec.stderr.toString();
expect(errs).toBe("");
expect(exec.success).toBeTrue();
return exec.stdout.toString();
}
Loading