Line data Source code
1 : /**
2 : Copyright (c) 2024 Stappler LLC <admin@stappler.dev>
3 :
4 : Permission is hereby granted, free of charge, to any person obtaining a copy
5 : of this software and associated documentation files (the "Software"), to deal
6 : in the Software without restriction, including without limitation the rights
7 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 : copies of the Software, and to permit persons to whom the Software is
9 : furnished to do so, subject to the following conditions:
10 :
11 : The above copyright notice and this permission notice shall be included in
12 : all copies or substantial portions of the Software.
13 :
14 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 : THE SOFTWARE.
21 : **/
22 :
23 : #include "SPWebWasmComponent.h"
24 :
25 : namespace STAPPLER_VERSIONIZED stappler::web {
26 :
27 0 : WasmComponent *WasmComponent::load(const Host &serv, const HostComponentInfo &info, wasm::Module *mod) {
28 0 : auto inst = Rc<wasm::ModuleInstance>::create(mod);
29 0 : if (!inst) {
30 0 : return nullptr;
31 : }
32 :
33 0 : auto env = Rc<wasm::ExecEnv>::create(inst);
34 :
35 0 : auto sym = inst->lookup(info.symbol);
36 0 : if (!sym) {
37 0 : return nullptr;
38 : }
39 :
40 0 : WasmComponent *result = nullptr;
41 :
42 0 : auto hostHandle = inst->addHandle(&serv);
43 0 : auto infoHandle = inst->addHandle(&info);
44 :
45 : wasm_val_t args[2] = {
46 0 : wasm::MakeValue(hostHandle),
47 0 : wasm::MakeValue(infoHandle),
48 : };
49 :
50 0 : auto idx = sym->call1(env, makeSpanView(args, 2));
51 0 : if (idx.kind == WASM_I32 && idx.of.i32 > 0) {
52 0 : result = inst->getObject<WasmComponent>(idx.of.i32);
53 : }
54 :
55 0 : inst->removeHandle(hostHandle);
56 0 : inst->removeHandle(infoHandle);
57 :
58 0 : return result;
59 0 : }
60 :
61 0 : WasmComponent::WasmComponent(const Host &serv, const HostComponentInfo &info, wasm::ExecEnv *env, WasmData &&data)
62 0 : : HostComponent(serv, info), _env(env), _wasmData(move(data)) {
63 :
64 0 : }
65 :
66 0 : void WasmComponent::handleChildInit(const Host &host) {
67 0 : HostComponent::handleChildInit(host);
68 :
69 0 : if (_wasmData.childInitCallback != 0) {
70 0 : std::unique_lock lock(_mutex);
71 0 : auto hostHandle = _env->getInstance()->addHandle(&host);
72 :
73 0 : uint32_t args[2] = { _wasmData.userdata, hostHandle };
74 0 : _env->callIndirect(_wasmData.childInitCallback, 2, args);
75 :
76 0 : _env->getInstance()->removeHandle(hostHandle);
77 0 : }
78 0 : }
79 :
80 0 : void WasmComponent::handleStorageInit(const Host &host, const db::Adapter &a) {
81 0 : HostComponent::handleStorageInit(host, a);
82 :
83 0 : if (_wasmData.storageInitCallback != 0) {
84 0 : std::unique_lock lock(_mutex);
85 0 : auto hostHandle = _env->getInstance()->addHandle(&host);
86 0 : auto adapterHandle = _env->getInstance()->addHandle(&a);
87 :
88 0 : uint32_t args[3] = { _wasmData.userdata, hostHandle, adapterHandle };
89 0 : _env->callIndirect(_wasmData.storageInitCallback, 3, args);
90 :
91 0 : _env->getInstance()->removeHandle(adapterHandle);
92 0 : _env->getInstance()->removeHandle(hostHandle);
93 0 : }
94 0 : }
95 :
96 0 : void WasmComponent::initTransaction(db::Transaction &t) {
97 0 : HostComponent::initTransaction(t);
98 0 : }
99 :
100 0 : void WasmComponent::handleHeartbeat(const Host &host) {
101 0 : HostComponent::handleHeartbeat(host);
102 :
103 0 : if (_wasmData.heartbeatCallback != 0) {
104 0 : std::unique_lock lock(_mutex);
105 0 : auto hostHandle = _env->getInstance()->addHandle(&host);
106 :
107 0 : uint32_t args[2] = { _wasmData.userdata, hostHandle };
108 0 : _env->callIndirect(_wasmData.heartbeatCallback, 2, args);
109 :
110 0 : _env->getInstance()->removeHandle(hostHandle);
111 0 : }
112 0 : }
113 :
114 :
115 : }
|