-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathzigdom.js
164 lines (143 loc) · 3.92 KB
/
zigdom.js
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const getString = function(ptr, len) {
const slice = zigdom.exports.memory.buffer.slice(ptr, ptr + len);
const textDecoder = new TextDecoder();
return textDecoder.decode(slice);
};
const pushObject = function(object) {
return zigdom.objects.push(object);
};
const getObject = function(objId) {
return zigdom.objects[objId - 1];
};
const dispatch = function(eventId) {
return function() {
zigdom.exports.dispatchEvent(eventId);
};
};
const elementSetAttribute = function(
node_id,
name_ptr,
name_len,
value_ptr,
value_len
) {
const node = getObject(node_id);
const attribute_name = getString(name_ptr, name_len);
const value = getString(value_ptr, value_len);
node[attribute_name] = value;
};
const elementGetAttribute = function(
node_id,
name_ptr,
name_len,
result_address_ptr,
result_address_len_ptr
) {
const node = getObject(node_id);
const attribute_name = getString(name_ptr, name_len);
const result = node[attribute_name];
// convert result into Uint8Array
const textEncoder = new TextEncoder();
const resultArray = textEncoder.encode(result);
var len = resultArray.length;
if (len === 0) {
return false;
}
// allocate required number of bytes
const ptr = zigdom.exports._wasm_alloc(len);
if (ptr === 0) {
throw "Cannot allocate memory";
}
// write the array to the memory
const mem_result = new DataView(zigdom.exports.memory.buffer, ptr, len);
for (let i = 0; i < len; ++i) {
mem_result.setUint8(i, resultArray[i], true);
}
// write the address of the result array to result_address_ptr
const mem_result_address = new DataView(
zigdom.exports.memory.buffer,
result_address_ptr,
32 / 8
);
mem_result_address.setUint32(0, ptr, true);
//write the size of the result array to result_address_ptr_len_ptr
const mem_result_address_len = new DataView(
zigdom.exports.memory.buffer,
result_address_len_ptr,
32 / 8
);
mem_result_address_len.setUint32(0, len, true);
// return if success? (optional)
return true;
};
const eventTargetAddEventListener = function(
objId,
event_ptr,
event_len,
eventId
) {
const node = getObject(objId);
const ev = getString(event_ptr, event_len);
node.addEventListener(ev, dispatch(eventId));
};
const documentQuerySelector = function(selector_ptr, selector_len) {
const selector = getString(selector_ptr, selector_len);
return pushObject(document.querySelector(selector));
};
const documentCreateElement = function(tag_name_ptr, tag_name_len) {
const tag_name = getString(tag_name_ptr, tag_name_len);
return pushObject(document.createElement(tag_name));
};
const documentCreateTextNode = function(data_ptr, data_len) {
data = getString(data_ptr, data_len);
return pushObject(document.createTextNode(data));
};
const nodeAppendChild = function(node_id, child_id) {
const node = getObject(node_id);
const child = getObject(child_id);
if (node === undefined || child === undefined) {
return 0;
}
return pushObject(node.appendChild(child));
};
const windowAlert = function(msg_ptr, msg_len) {
const msg = getString(msg_ptr, msg_len);
alert(msg);
};
const zigReleaseObject = function(object_id) {
zigdom.objects[object_id - 1] = undefined;
};
const launch = function(result) {
zigdom.exports = result.instance.exports;
if (!zigdom.exports.launch_export()) {
throw "Launch Error";
}
};
var zigdom = {
objects: [],
imports: {
document: {
query_selector: documentQuerySelector,
create_element: documentCreateElement,
create_text_node: documentCreateTextNode
},
element: {
set_attribute: elementSetAttribute,
get_attribute: elementGetAttribute
},
event_target: {
add_event_listener: eventTargetAddEventListener
},
node: {
append_child: nodeAppendChild
},
window: {
alert: windowAlert
},
zig: {
release_object: zigReleaseObject
}
},
launch: launch,
exports: undefined
};