aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js/bindings/Serialization.cpp
diff options
context:
space:
mode:
authorGravatar dave caruso <me@paperdave.net> 2023-10-16 20:01:24 -0700
committerGravatar GitHub <noreply@github.com> 2023-10-16 20:01:24 -0700
commita3958190e8f106adca7fbf4ba2605056cb22aced (patch)
tree475057061d3470f1dc4d06b901d6bad0b898cb09 /src/bun.js/bindings/Serialization.cpp
parent6504bfef74b552aa834324adfe102c9ba0193039 (diff)
downloadbun-a3958190e8f106adca7fbf4ba2605056cb22aced.tar.gz
bun-a3958190e8f106adca7fbf4ba2605056cb22aced.tar.zst
bun-a3958190e8f106adca7fbf4ba2605056cb22aced.zip
fix(runtime): improve IPC reliability + organization pass on that code (#6475)
* dfghj * Handle messages that did not finish * tidy * ok * a * Merge remote-tracking branch 'origin/main' into dave/ipc-fixes * test failures --------- Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
Diffstat (limited to 'src/bun.js/bindings/Serialization.cpp')
-rw-r--r--src/bun.js/bindings/Serialization.cpp32
1 files changed, 21 insertions, 11 deletions
diff --git a/src/bun.js/bindings/Serialization.cpp b/src/bun.js/bindings/Serialization.cpp
index 89937ebbb..fdaf2ab75 100644
--- a/src/bun.js/bindings/Serialization.cpp
+++ b/src/bun.js/bindings/Serialization.cpp
@@ -8,9 +8,15 @@
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)
+// Must be synced with bindings.zig's JSValue.SerializedScriptValue.External
+struct SerializedValueSlice {
+ uint8_t* bytes;
+ size_t size;
+ WebCore::SerializedScriptValue* value;
+};
+
+/// Returns a "slice" that also contains a pointer to the SerializedScriptValue. Must be freed by the caller
+extern "C" SerializedValueSlice Bun__serializeJSValue(JSGlobalObject* globalObject, EncodedJSValue encodedValue)
{
JSValue value = JSValue::decode(encodedValue);
@@ -25,19 +31,23 @@ extern "C" bool Bun__serializeJSValueForSubprocess(JSGlobalObject* globalObject,
if (serialized.hasException()) {
WebCore::propagateException(*globalObject, scope,
serialized.releaseException());
- RELEASE_AND_RETURN(scope, false);
+ RELEASE_AND_RETURN(scope, { 0 });
}
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);
+ auto bytes = serializedValue->wireBytes();
- RELEASE_AND_RETURN(scope, true);
+ return {
+ bytes.data(),
+ bytes.size(),
+ &serializedValue.leakRef(),
+ };
+}
+
+extern "C" void Bun__SerializedScriptSlice__free(SerializedScriptValue* value)
+{
+ delete value;
}
extern "C" EncodedJSValue Bun__JSValue__deserialize(JSGlobalObject* globalObject, const uint8_t* bytes, size_t size)