-
-
Notifications
You must be signed in to change notification settings - Fork 561
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
Bytes as ArrayBuffer #1590
Bytes as ArrayBuffer #1590
Conversation
Nice! Here's some feedback from the top of my head. I think it would also be fine if we rename
The constructed object is not properly initialized, I'd suggest adding new public ArrayBufferInstance Construct(byte[] data)
{
var obj = OrdinaryCreateFromConstructor(
this,
static intrinsics => intrinsics.ArrayBuffer.PrototypeObject,
static (engine, _, state) => new ArrayBufferInstance(engine, state!),
data);
return obj;
} And changing your converter logic to: if (value is byte[] bytes)
{
result = engine.Realm.Intrinsics.ArrayBuffer.Construct(bytes);
return true;
} Note, now
I think there might be small problem with ambiguity here. You could also want to have your
Everything what makes Jint more usable in interop scenarios sounds good to me. |
Thank you for a prompt and in-depth reply @lahma . This is my first more serious work with Jint codebase so that I'm stepping into unfamiliar territory. Let me dive deeper with your comments in mind and get this PR altered over the weekend. |
With #1591 I did some preliminary work and did the rename and expose of type. If you find it useful for future, could also unseal it if there's some benefit with different backing store implementations. So if you rebase, you should have things readily available. |
I stumbled upon this PR via this discussion while looking for a way to pass in CLR byte[] fileBytes = await LoadLargeFileAsync();
engine.SetValue("fileBytes", fileBytes);
var result = engine.Evaluate("return fileBytes;");
var bytes = result.ToObject(); // becomes object[], but I want byte[]. Currently, I am solving this using a custom converter that is close to implementation as proposed via this PR: internal class ByteArrayConverter : IObjectConverter
{
public bool TryConvert(Engine engine, object value, [NotNullWhen(true)] out JsValue? result)
{
if (value is byte[] bytes)
{
result = engine.Intrinsics.Uint8Array.Construct(bytes);
return true;
}
result = JsValue.Null;
return false;
}
} However, looking at the The proposed PR, on the other hand, using a new @Scooletz do you still want to complete this PR, and/or would you like me to take it from here? I could fork your fork or perhaps you can give me push access to your fork so that I can update this PR directly. |
@lahma It looks like that if I can somehow instantiate the renamed I'm also wondering if it would make sense to update |
I'll try to create a intermediate step PR to improve APIs to allow passing in existing items. |
@sfmskywalker please see #1890 and whether it would help you achieve your goals. The automatic |
@lahma Your PR is spot-on to achieving my goals 🙌🏻 Thank you for the super fast turnaround! |
I don't have capacity to push it further. Should I close? |
I'll close this as I think the most important bits are now supported. Thanks for bringing this up and pushing the capabilities further. |
This PR introduces a capability to convert raw byte chunks like
byte[]
as anArrayBuffer
. It does it by implementing an object converter directly inJint
core project. This is done to not make theArrayBufferInstance
public and keep the API change as minimal.Open questions:
Memory<byte>
which would require offset bookkeeping in the ArrayBuffer (I can implement it).