diff options
author | 2023-09-07 04:58:44 -0700 | |
---|---|---|
committer | 2023-09-07 04:58:44 -0700 | |
commit | 57a06745a48093c25d0f4729ccea41a918d6427d (patch) | |
tree | ac2568d5c98918d6364d2a9667c164cd3f3b3867 /src/bun.js/bindings/Serialization.cpp | |
parent | 4360ec83b4146e15344b304573795f084f86a7c2 (diff) | |
download | bun-57a06745a48093c25d0f4729ccea41a918d6427d.tar.gz bun-57a06745a48093c25d0f4729ccea41a918d6427d.tar.zst bun-57a06745a48093c25d0f4729ccea41a918d6427d.zip |
Progress for Next.js (#4468)
* L
* ipc
* asdfghjkl
* dfghjk
* it works!
* types
* patches for next.js
* sdfghj
* wsdfgn,./
* this
* yolo
* okay loser
* asdfghjk
* add some more APIs
* MESS
* sdfghjkl
* remove native events from streams
* stuff
* remove lazy(primordials) test
* debugging
* okay
* less fake extensions object
* fix `Buffer.toString()` args logic
* fix deserialize
* make tests work
* add test for `Buffer.toString` args
* Update server.zig
* remove test
* update test
* Update spawn-streaming-stdin.test.ts
* fix linux build
* Update fs.test.ts
* cli message improvements
* dfshaj
* Fix fs.watch bug maybe?
* remove
---------
Co-authored-by: Dylan Conway <dylan.conway567@gmail.com>
Diffstat (limited to 'src/bun.js/bindings/Serialization.cpp')
-rw-r--r-- | src/bun.js/bindings/Serialization.cpp | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/bun.js/bindings/Serialization.cpp b/src/bun.js/bindings/Serialization.cpp new file mode 100644 index 000000000..89937ebbb --- /dev/null +++ b/src/bun.js/bindings/Serialization.cpp @@ -0,0 +1,49 @@ +#include "root.h" +#include "headers-handwritten.h" +#include "ExceptionOr.h" +#include "MessagePort.h" +#include "SerializedScriptValue.h" +#include "JSDOMExceptionHandling.h" + +using namespace JSC; +using namespace WebCore; + +/// This is used for Bun.spawn() IPC because otherwise we would have to copy the data once to get it to zig, then write it. +/// Returns `true` on success, `false` on failure + throws a JS error. +extern "C" bool Bun__serializeJSValueForSubprocess(JSGlobalObject* globalObject, EncodedJSValue encodedValue, int fd) +{ + JSValue value = JSValue::decode(encodedValue); + + Vector<JSC::Strong<JSC::JSObject>> transferList; + Vector<RefPtr<MessagePort>> dummyPorts; + ExceptionOr<Ref<SerializedScriptValue>> serialized = SerializedScriptValue::create(*globalObject, value, WTFMove(transferList), + dummyPorts); + + auto& vm = globalObject->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + + if (serialized.hasException()) { + WebCore::propagateException(*globalObject, scope, + serialized.releaseException()); + RELEASE_AND_RETURN(scope, false); + } + + auto serializedValue = serialized.releaseReturnValue(); + auto bytes = serializedValue.ptr()->wireBytes(); + + uint8_t id = 2; // IPCMessageType.SerializedMessage + write(fd, &id, sizeof(uint8_t)); + uint32_t size = bytes.size(); + write(fd, &size, sizeof(uint32_t)); + write(fd, bytes.data(), size); + + RELEASE_AND_RETURN(scope, true); +} + +extern "C" EncodedJSValue Bun__JSValue__deserialize(JSGlobalObject* globalObject, const uint8_t* bytes, size_t size) +{ + Vector<uint8_t> vector(bytes, size); + /// ?! did i just give ownership of these bytes to JSC? + auto scriptValue = SerializedScriptValue::createFromWireBytes(WTFMove(vector)); + return JSValue::encode(scriptValue->deserialize(*globalObject, globalObject)); +}
\ No newline at end of file |