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

Add native StringDecoder #1188

Merged
merged 3 commits into from
Sep 1, 2022
Merged

Conversation

zhuzilin
Copy link
Collaborator

@zhuzilin zhuzilin commented Sep 1, 2022

The code is a migration of https://github.com/nodejs/string_decoder. And the tests are from https://github.com/nodejs/node/blob/main/test/parallel/test-string-decoder.js.

Note that I also fixed the buffer.toString("utf16le").

Thank you for your time on this PR :)

@github-actions github-actions bot added packages:bun needs tests Something that needs more testing labels Sep 1, 2022
encoding = opt.value();
}
}
JSStringDecoderPrototype* prototype = JSStringDecoderPrototype::create(
Copy link
Collaborator

@Jarred-Sumner Jarred-Sumner Sep 1, 2022

Choose a reason for hiding this comment

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

We should use LazyClassStructure

If we create a new structure for the prototype each time, the JIT will never optimize this code so it will be slow

I'm assuming more than one instance can be created

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fixed.

JSStringDecoderPrototype* prototype = JSStringDecoderPrototype::create(
vm, globalObject, reinterpret_cast<Zig::GlobalObject*>(globalObject)->JSStringDecoderPrototypeStructure());
JSStringDecoder* stringDecoder = JSStringDecoder::create(
vm, globalObject, JSStringDecoder::createStructure(vm, globalObject, prototype), encoding);
Copy link
Collaborator

@Jarred-Sumner Jarred-Sumner Sep 1, 2022

Choose a reason for hiding this comment

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

This structure should be the LazyProperty, not the prototype's structure

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

oh... ok...

static const HashTableValue JSStringDecoderPrototypeTableValues[]
= {
{ "write"_s, static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, jsStringDecoderPrototypeFunction_write, 1 } },
{ "end"_s, static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, jsStringDecoderPrototypeFunction_end, 1 } },
Copy link
Collaborator

Choose a reason for hiding this comment

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

we don't expose "constructor" here?

Copy link
Collaborator

Choose a reason for hiding this comment

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

image

Copy link
Collaborator

Choose a reason for hiding this comment

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

image

Copy link
Collaborator

Choose a reason for hiding this comment

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

looks like it should be LazyClassStructure after all (LazyClassStructure will add constructor and prototype there)

}
int32_t offset = callFrame->uncheckedArgument(1).toInt32(lexicalGlobalObject);
RETURN_IF_EXCEPTION(throwScope, JSC::JSValue::encode(JSC::jsUndefined()));
if (offset > view->length())
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
if (offset > view->length())
if (offset >= view->length())

{
return WebCore::subspaceForImpl<JSStringDecoderConstructor, UseCustomHeapCellType::No>(
vm,
[](auto& spaces) { return spaces.m_clientSubspaceForStringDecoderConstructor.get(); },
Copy link
Collaborator

Choose a reason for hiding this comment

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

Shouldn't need a subspace for the constructor since no functions/properties are added

var output = allocator.alloc(u16, len / 2) catch return ZigString.init("Out of memory").toErrorInstance(global);
var i : usize = 0;
while (i < len / 2) : (i += 1) {
output[i] = (@intCast(u16, input[2 * i + 1]) << 8) + @intCast(u16, input[2 * i]);
Copy link
Collaborator

Choose a reason for hiding this comment

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

This code is not fast, but I will clean it up

@Jarred-Sumner Jarred-Sumner merged commit 700c31d into oven-sh:main Sep 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs tests Something that needs more testing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants