-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathStructuredClone.cpp
More file actions
61 lines (48 loc) · 1.74 KB
/
Copy pathStructuredClone.cpp
File metadata and controls
61 lines (48 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "StructuredClone.h"
#include "BuiltinLoader.h"
#include "Helpers.h"
#include "StructuredSerialization.h"
using namespace v8;
namespace tns {
namespace {
// binding.clone(value, transferArrayOrUndefined): serialize and deserialize in
// this one isolate, which is what StructuredDeserialize(
// StructuredSerializeWithTransfer(...)) amounts to when there is no second
// agent involved.
void CloneCallback(const FunctionCallbackInfo<Value>& info) {
Isolate* isolate = info.GetIsolate();
Local<Context> context = isolate->GetCurrentContext();
Local<Value> value =
info.Length() > 0 ? info[0] : v8::Undefined(isolate).As<Value>();
Local<Value> transferList =
info.Length() > 1 ? info[1] : v8::Undefined(isolate).As<Value>();
serialization::SerializedValue serialized;
if (serialized
.Serialize(isolate, context, value, transferList,
serialization::HostObjectPolicy::kReject)
.IsNothing()) {
return;
}
Local<Value> result;
if (!serialized.Deserialize(isolate, context).ToLocal(&result)) {
return;
}
info.GetReturnValue().Set(result);
}
} // namespace
void StructuredClone::Init(Local<Context> context) {
Isolate* isolate = v8::Isolate::GetCurrent();
Local<v8::Function> clone;
bool success = v8::Function::New(context, CloneCallback).ToLocal(&clone);
tns::Assert(success, isolate);
Local<Object> binding = Object::New(isolate);
success = binding->Set(context, tns::ToV8String(isolate, "clone"), clone)
.FromMaybe(false);
tns::Assert(success, isolate);
Local<Value> result;
success =
BuiltinLoader::RunBuiltin(context, BuiltinId::kStructuredClone, binding)
.ToLocal(&result);
tns::Assert(success, isolate);
}
} // namespace tns