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

napi: Add string latin1 apis #192

Closed
Closed
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
50 changes: 50 additions & 0 deletions src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1150,6 +1150,25 @@ napi_status napi_create_array_with_length(napi_env env,
return GET_RETURN_STATUS();
}

napi_status napi_create_string_latin1(napi_env env,
const char* str,
size_t length,
napi_value* result) {
NAPI_PREAMBLE(env);
CHECK_ARG(result);

auto isolate = v8impl::V8IsolateFromJsEnv(env);
auto str_maybe =
v8::String::NewFromOneByte(isolate,
reinterpret_cast<const uint8_t*>(str),
v8::NewStringType::kInternalized,
length);
CHECK_MAYBE_EMPTY(str_maybe, napi_generic_failure);

*result = v8impl::JsValueFromV8LocalValue(str_maybe.ToLocalChecked());
return GET_RETURN_STATUS();
}

napi_status napi_create_string_utf8(napi_env env,
const char* str,
size_t length,
Expand Down Expand Up @@ -1627,6 +1646,37 @@ napi_status napi_get_value_string_length(napi_env env,
return GET_RETURN_STATUS();
}

// Copies a JavaScript string into a LATIN-1 string buffer. The result is the
// number of bytes copied into buf, including the null terminator. If bufsize
// is insufficient, the string will be truncated, including a null terminator.
// If buf is NULL, this method returns the length of the string (in bytes)
// via the result parameter.
// The result argument is optional unless buf is NULL.
napi_status napi_get_value_string_latin1(napi_env env,
napi_value value,
char* buf,
size_t bufsize,
size_t* result) {
NAPI_PREAMBLE(env);

v8::Local<v8::Value> val = v8impl::V8LocalValueFromJsValue(value);
RETURN_STATUS_IF_FALSE(val->IsString(), napi_string_expected);

if (!buf) {
CHECK_ARG(result);
*result = val.As<v8::String>()->Utf8Length();
} else {
int copied = val.As<v8::String>()->WriteOneByte(
reinterpret_cast<uint8_t*>(buf), 0, bufsize, v8::String::NO_OPTIONS);

if (result != nullptr) {
*result = copied;
}
}

return GET_RETURN_STATUS();
}

// Copies a JavaScript string into a UTF-8 string buffer. The result is the
// number of bytes copied into buf, including the null terminator. If bufsize
// is insufficient, the string will be truncated, including a null terminator.
Expand Down
11 changes: 11 additions & 0 deletions src/node_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ NAPI_EXTERN napi_status napi_create_array_with_length(napi_env env,
NAPI_EXTERN napi_status napi_create_number(napi_env env,
double value,
napi_value* result);
NAPI_EXTERN napi_status napi_create_string_latin1(napi_env env,
const char* str,
size_t length,
napi_value* result);
NAPI_EXTERN napi_status napi_create_string_utf8(napi_env env,
const char* str,
size_t length,
Expand Down Expand Up @@ -170,6 +174,13 @@ NAPI_EXTERN napi_status napi_get_value_string_length(napi_env env,
napi_value value,
size_t* result);

// Copies LATIN-1 encoded bytes from a string into a buffer.
NAPI_EXTERN napi_status napi_get_value_string_latin1(napi_env env,
napi_value value,
char* buf,
size_t bufsize,
size_t* result);

// Copies UTF-8 encoded bytes from a string into a buffer.
NAPI_EXTERN napi_status napi_get_value_string_utf8(napi_env env,
napi_value value,
Expand Down
15 changes: 11 additions & 4 deletions test/addons-napi/test_string/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,28 @@ const assert = require('assert');
const test_string = require(`./build/${common.buildType}/test_string`);

const str1 = 'hello world';

This comment was marked as off-topic.

assert.strictEqual(test_string.Copy(str1), str1);
assert.strictEqual(test_string.TestLatin1(str1), str1);
assert.strictEqual(test_string.TestUtf8(str1), str1);
assert.strictEqual(test_string.TestUtf16(str1), str1);
assert.strictEqual(test_string.Length(str1), 11);
assert.strictEqual(test_string.Utf8Length(str1), 11);

const str2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
assert.strictEqual(test_string.Copy(str2), str2);
assert.strictEqual(test_string.TestLatin1(str2), str2);
assert.strictEqual(test_string.TestUtf8(str2), str2);
assert.strictEqual(test_string.TestUtf16(str2), str2);
assert.strictEqual(test_string.Length(str2), 62);
assert.strictEqual(test_string.Utf8Length(str2), 62);

const str3 = '?!@#$%^&*()_+-=[]{}/.,<>\'"\\';
assert.strictEqual(test_string.Copy(str3), str3);
assert.strictEqual(test_string.TestLatin1(str3), str3);
assert.strictEqual(test_string.TestUtf8(str3), str3);
assert.strictEqual(test_string.TestUtf16(str3), str3);
assert.strictEqual(test_string.Length(str3), 27);
assert.strictEqual(test_string.Utf8Length(str3), 27);

const str4 = '\u{2003}\u{2101}\u{2001}';
assert.strictEqual(test_string.Copy(str4), str4);
assert.strictEqual(test_string.TestUtf8(str4), str4);
assert.strictEqual(test_string.TestUtf16(str4), str4);
assert.strictEqual(test_string.Length(str4), 3);
assert.strictEqual(test_string.Utf8Length(str4), 9);
86 changes: 84 additions & 2 deletions test/addons-napi/test_string/test_string.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,46 @@
#include <node_api.h>

void Copy(napi_env env, napi_callback_info info) {
void TestLatin1(napi_env env, napi_callback_info info) {
napi_status status;

size_t argc;
status = napi_get_cb_args_length(env, info, &argc);
if (status != napi_ok) return;

if (argc < 1) {
napi_throw_type_error(env, "Wrong number of arguments");
return;
}

napi_value args[1];
status = napi_get_cb_args(env, info, args, 1);
if (status != napi_ok) return;

napi_valuetype valuetype;
status = napi_typeof(env, args[0], &valuetype);
if (status != napi_ok) return;

if (valuetype != napi_string) {
napi_throw_type_error(env, "Wrong type of argments. Expects a string.");
return;
}

char buffer[128];
int buffer_size = 128;

status =
napi_get_value_string_latin1(env, args[0], buffer, buffer_size, NULL);
if (status != napi_ok) return;

napi_value output;
status = napi_create_string_latin1(env, buffer, -1, &output);

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

if (status != napi_ok) return;

status = napi_set_return_value(env, info, output);
if (status != napi_ok) return;
}

void TestUtf8(napi_env env, napi_callback_info info) {
napi_status status;

size_t argc;
Expand Down Expand Up @@ -40,6 +80,46 @@ void Copy(napi_env env, napi_callback_info info) {
if (status != napi_ok) return;
}

void TestUtf16(napi_env env, napi_callback_info info) {
napi_status status;

size_t argc;
status = napi_get_cb_args_length(env, info, &argc);
if (status != napi_ok) return;

if (argc < 1) {
napi_throw_type_error(env, "Wrong number of arguments");
return;
}

napi_value args[1];
status = napi_get_cb_args(env, info, args, 1);
if (status != napi_ok) return;

napi_valuetype valuetype;
status = napi_typeof(env, args[0], &valuetype);
if (status != napi_ok) return;

if (valuetype != napi_string) {
napi_throw_type_error(env, "Wrong type of argments. Expects a string.");
return;
}

char buffer[128];
int buffer_size = 128;

status =
napi_get_value_string_utf16(env, args[0], buffer, buffer_size, NULL);
if (status != napi_ok) return;

napi_value output;
status = napi_create_string_utf16(env, buffer, -1, &output);
if (status != napi_ok) return;

status = napi_set_return_value(env, info, output);
if (status != napi_ok) return;
}

void Length(napi_env env, napi_callback_info info) {
napi_status status;

Expand Down Expand Up @@ -121,7 +201,9 @@ void Init(napi_env env, napi_value exports, napi_value module, void* priv) {
napi_status status;

napi_property_descriptor properties[] = {
DECLARE_NAPI_METHOD("Copy", Copy),
DECLARE_NAPI_METHOD("TestLatin1", TestLatin1),
DECLARE_NAPI_METHOD("TestUtf8", TestUtf8),
DECLARE_NAPI_METHOD("TestUtf16", TestUtf16),
DECLARE_NAPI_METHOD("Length", Length),
DECLARE_NAPI_METHOD("Utf8Length", Utf8Length),
};
Expand Down