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

src: add a method to get string encoding info #56147

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
39 changes: 39 additions & 0 deletions doc/api/v8.md
Original file line number Diff line number Diff line change
Expand Up @@ -1304,6 +1304,45 @@ setTimeout(() => {
}, 1000);
```

## `v8.isStringOneByteRepresentation(content)`

<!-- YAML
added: REPLACEME
-->

* `content` {string}
* Returns: {boolean}

V8 only supports `Latin-1/ISO-8859-1` and `UTF16` as the underlying representation of a string.
If the `content` uses `Latin-1/ISO-8859-1` as the underlying representation, this function will return true;
otherwise, it returns false.

If this method returns false, that does not mean that the string contains some characters not in `Latin-1/ISO-8859-1`.
Sometimes a `Latin-1` string may also be represented as `UTF16`.

```js
const { isStringOneByteRepresentation } = require('node:v8');

const Encoding = {
latin1: 1,
utf16le: 2,
};
const buffer = Buffer.alloc(100);
function writeString(input) {
if (isStringOneByteRepresentation(input)) {
buffer.writeUint8(Encoding.latin1);
buffer.writeUint32LE(input.length, 1);
buffer.write(input, 5, 'latin1');
} else {
buffer.writeUint8(Encoding.utf16le);
buffer.writeUint32LE(input.length * 2, 1);
buffer.write(input, 5, 'utf16le');
}
}
writeString('hello');
writeString('你好');
```

[HTML structured clone algorithm]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm
[Hook Callbacks]: #hook-callbacks
[V8]: https://developers.google.com/v8/
Expand Down
13 changes: 13 additions & 0 deletions lib/v8.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ const binding = internalBinding('v8');
const {
cachedDataVersionTag,
setFlagsFromString: _setFlagsFromString,
isStringOneByteRepresentation: _isStringOneByteRepresentation,
updateHeapStatisticsBuffer,
updateHeapSpaceStatisticsBuffer,
updateHeapCodeStatisticsBuffer,
Expand Down Expand Up @@ -155,6 +156,17 @@ function setFlagsFromString(flags) {
_setFlagsFromString(flags);
}

/**
* Return whether this string uses one byte as underlying representation or not.
* @param {string} content
* @returns {boolean}
*/
function isStringOneByteRepresentation(content) {
validateString(content, 'content');
return _isStringOneByteRepresentation(content);
}


/**
* Gets the current V8 heap statistics.
* @returns {{
Expand Down Expand Up @@ -439,4 +451,5 @@ module.exports = {
startupSnapshot,
setHeapSnapshotNearHeapLimit,
GCProfiler,
isStringOneByteRepresentation,
};
4 changes: 4 additions & 0 deletions src/node_external_reference.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ namespace node {

using CFunctionCallbackWithOneByteString =
uint32_t (*)(v8::Local<v8::Value>, const v8::FastOneByteString&);

using CFunctionCallbackReturnBool = bool (*)(v8::Local<v8::Value> unused,
v8::Local<v8::Value> receiver);
using CFunctionCallback = void (*)(v8::Local<v8::Value> unused,
v8::Local<v8::Value> receiver);
using CFunctionCallbackReturnDouble =
Expand Down Expand Up @@ -90,6 +93,7 @@ class ExternalReferenceRegistry {
#define ALLOWED_EXTERNAL_REFERENCE_TYPES(V) \
V(CFunctionCallback) \
V(CFunctionCallbackWithOneByteString) \
V(CFunctionCallbackReturnBool) \
V(CFunctionCallbackReturnDouble) \
V(CFunctionCallbackReturnInt32) \
V(CFunctionCallbackValueReturnDouble) \
Expand Down
28 changes: 28 additions & 0 deletions src/node_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
namespace node {
namespace v8_utils {
using v8::Array;
using v8::CFunction;
using v8::Context;
using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
Expand Down Expand Up @@ -238,6 +239,23 @@ void SetFlagsFromString(const FunctionCallbackInfo<Value>& args) {
V8::SetFlagsFromString(*flags, static_cast<size_t>(flags.length()));
}

static void IsStringOneByteRepresentation(
const FunctionCallbackInfo<Value>& args) {
CHECK_EQ(args.Length(), 1);
CHECK(args[0]->IsString());
bool is_one_byte = args[0].As<String>()->IsOneByte();
args.GetReturnValue().Set(is_one_byte);
}

static bool FastIsStringOneByteRepresentation(Local<Value> receiver,
const Local<Value> target) {
CHECK(target->IsString());
return target.As<String>()->IsOneByte();
}

CFunction fast_is_string_one_byte_representation_(
CFunction::Make(FastIsStringOneByteRepresentation));

static const char* GetGCTypeName(v8::GCType gc_type) {
switch (gc_type) {
case v8::GCType::kGCTypeScavenge:
Expand Down Expand Up @@ -479,6 +497,13 @@ void Initialize(Local<Object> target,
// Export symbols used by v8.setFlagsFromString()
SetMethod(context, target, "setFlagsFromString", SetFlagsFromString);

// Export symbols used by v8.isStringOneByteRepresentation()
SetFastMethodNoSideEffect(context,
target,
"isStringOneByteRepresentation",
IsStringOneByteRepresentation,
&fast_is_string_one_byte_representation_);

// GCProfiler
Local<FunctionTemplate> t =
NewFunctionTemplate(env->isolate(), GCProfiler::New);
Expand All @@ -498,6 +523,9 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(GCProfiler::New);
registry->Register(GCProfiler::Start);
registry->Register(GCProfiler::Stop);
registry->Register(IsStringOneByteRepresentation);
registry->Register(FastIsStringOneByteRepresentation);
registry->Register(fast_is_string_one_byte_representation_.GetTypeInfo());
}

} // namespace v8_utils
Expand Down
36 changes: 36 additions & 0 deletions test/parallel/test-v8-string-is-one-byte-representation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';
require('../common');
const assert = require('assert');
const { isStringOneByteRepresentation } = require('v8');

[
undefined,
null,
false,
5n,
5,
Symbol(),
() => {},
{},
].forEach((value) => {
assert.throws(
() => { isStringOneByteRepresentation(value); },
/The "content" argument must be of type string/
);
});

{
const latin1String = 'hello world!';
// Run this inside a for loop to trigger the fast API
for (let i = 0; i < 10_000; i++) {
assert.strictEqual(isStringOneByteRepresentation(latin1String), true);
}
}

{
const utf16String = '你好😀😃';
// Run this inside a for loop to trigger the fast API
for (let i = 0; i < 10_000; i++) {
assert.strictEqual(isStringOneByteRepresentation(utf16String), false);
}
}
Loading