Line data Source code
1 : /* Copyright (C) 2021 Wildfire Games.
2 : * This file is part of 0 A.D.
3 : *
4 : * 0 A.D. is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 2 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * 0 A.D. is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : #include "precompiled.h"
19 :
20 : #include "ps/Profile.h"
21 : #include "ScriptExceptions.h"
22 : #include "ScriptInterface.h"
23 : #include "ScriptRequest.h"
24 : #include "StructuredClone.h"
25 :
26 : // Ignore warnings in SM headers.
27 : #if GCC_VERSION || CLANG_VERSION
28 : # pragma GCC diagnostic push
29 : # pragma GCC diagnostic ignored "-Wunused-parameter"
30 : # pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
31 : #elif MSC_VERSION
32 : # pragma warning(push, 1)
33 : #endif
34 :
35 : #include "js/StructuredClone.h"
36 :
37 : #if GCC_VERSION || CLANG_VERSION
38 : # pragma GCC diagnostic pop
39 : #elif MSC_VERSION
40 : # pragma warning(pop)
41 : #endif
42 :
43 338 : Script::StructuredClone Script::WriteStructuredClone(const ScriptRequest& rq, JS::HandleValue v)
44 : {
45 676 : Script::StructuredClone ret(new JSStructuredCloneData(JS::StructuredCloneScope::SameProcess));
46 338 : JS::CloneDataPolicy policy;
47 338 : if (!JS_WriteStructuredClone(rq.cx, v, ret.get(), JS::StructuredCloneScope::SameProcess, policy, nullptr, nullptr, JS::UndefinedHandleValue))
48 : {
49 0 : debug_warn(L"Writing a structured clone with JS_WriteStructuredClone failed!");
50 0 : ScriptException::CatchPending(rq);
51 0 : return StructuredClone();
52 : }
53 :
54 338 : return ret;
55 : }
56 :
57 340 : void Script::ReadStructuredClone(const ScriptRequest& rq, const Script::StructuredClone& ptr, JS::MutableHandleValue ret)
58 : {
59 340 : JS::CloneDataPolicy policy;
60 340 : if (!JS_ReadStructuredClone(rq.cx, *ptr, JS_STRUCTURED_CLONE_VERSION, ptr->scope(), ret, policy, nullptr, nullptr))
61 0 : ScriptException::CatchPending(rq);
62 340 : }
63 :
64 3 : JS::Value Script::CloneValueFromOtherCompartment(const ScriptInterface& to, const ScriptInterface& from, JS::HandleValue val)
65 : {
66 6 : PROFILE("CloneValueFromOtherCompartment");
67 6 : Script::StructuredClone structuredClone;
68 : {
69 6 : ScriptRequest fromRq(from);
70 3 : structuredClone = WriteStructuredClone(fromRq, val);
71 : }
72 6 : ScriptRequest toRq(to);
73 6 : JS::RootedValue out(toRq.cx);
74 3 : ReadStructuredClone(toRq, structuredClone, &out);
75 6 : return out.get();
76 : }
77 :
78 330 : JS::Value Script::DeepCopy(const ScriptRequest& rq, JS::HandleValue val)
79 : {
80 660 : JS::RootedValue out(rq.cx);
81 330 : ReadStructuredClone(rq, WriteStructuredClone(rq, val), &out);
82 660 : return out.get();
83 : }
|