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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
/*global Module:true, Runtime*/
/*global HEAP32*/
/*global new_*/
/*global createNamedFunction*/
/*global readLatin1String, writeStringToMemory*/
/*global requireRegisteredType, throwBindingError, runDestructors*/
/*jslint sub:true*/ /* The symbols 'fromWireType' and 'toWireType' must be accessed via array notation to be closure-safe since craftInvokerFunction crafts functions as strings that can't be closured. */
var Module = Module || {};
var _emval_handle_array = [{}]; // reserve zero
var _emval_free_list = [];
// Public JS API
/** @expose */
Module.count_emval_handles = function() {
var count = 0;
for (var i = 1; i < _emval_handle_array.length; ++i) {
if (_emval_handle_array[i] !== undefined) {
++count;
}
}
return count;
};
/** @expose */
Module.get_first_emval = function() {
for (var i = 1; i < _emval_handle_array.length; ++i) {
if (_emval_handle_array[i] !== undefined) {
return _emval_handle_array[i];
}
}
return null;
};
// Private C++ API
var _emval_symbols = {}; // address -> string
function __emval_register_symbol(address) {
_emval_symbols[address] = readLatin1String(address);
}
function getStringOrSymbol(address) {
var symbol = _emval_symbols[address];
if (symbol === undefined) {
return readLatin1String(address);
} else {
return symbol;
}
}
function requireHandle(handle) {
if (!handle) {
throwBindingError('Cannot use deleted val. handle = ' + handle);
}
return _emval_handle_array[handle].value;
}
function __emval_register(value) {
var handle = _emval_free_list.length ?
_emval_free_list.pop() :
_emval_handle_array.length;
_emval_handle_array[handle] = {refcount: 1, value: value};
return handle;
}
function __emval_incref(handle) {
if (handle) {
_emval_handle_array[handle].refcount += 1;
}
}
function __emval_decref(handle) {
if (handle && 0 === --_emval_handle_array[handle].refcount) {
_emval_handle_array[handle] = undefined;
_emval_free_list.push(handle);
}
}
function __emval_run_destructors(handle) {
var destructors = _emval_handle_array[handle].value;
runDestructors(destructors);
__emval_decref(handle);
}
function __emval_new_array() {
return __emval_register([]);
}
function __emval_new_object() {
return __emval_register({});
}
function __emval_undefined() {
return __emval_register(undefined);
}
function __emval_null() {
return __emval_register(null);
}
function __emval_new_cstring(v) {
return __emval_register(getStringOrSymbol(v));
}
function __emval_take_value(type, argv) {
type = requireRegisteredType(type, '_emval_take_value');
var v = type['readValueFromPointer'](argv);
return __emval_register(v);
}
var __newers = {}; // arity -> function
function craftEmvalAllocator(argCount) {
/*This function returns a new function that looks like this:
function emval_allocator_3(constructor, argTypes, args) {
var argType0 = requireRegisteredType(HEAP32[(argTypes >> 2)], "parameter 0");
var arg0 = argType0.readValueFromPointer(args);
var argType1 = requireRegisteredType(HEAP32[(argTypes >> 2) + 1], "parameter 1");
var arg1 = argType1.readValueFromPointer(args + 8);
var argType2 = requireRegisteredType(HEAP32[(argTypes >> 2) + 2], "parameter 2");
var arg2 = argType2.readValueFromPointer(args + 16);
var obj = new constructor(arg0, arg1, arg2);
return __emval_register(obj);
} */
var argsList = "";
for(var i = 0; i < argCount; ++i) {
argsList += (i!==0?", ":"")+"arg"+i; // 'arg0, arg1, ..., argn'
}
var functionBody =
"return function emval_allocator_"+argCount+"(constructor, argTypes, args) {\n";
for(var i = 0; i < argCount; ++i) {
functionBody +=
"var argType"+i+" = requireRegisteredType(HEAP32[(argTypes >> 2) + "+i+"], \"parameter "+i+"\");\n" +
"var arg"+i+" = argType"+i+".readValueFromPointer(args);\n" +
"args += argType"+i+".argPackAdvance;\n";
}
functionBody +=
"var obj = new constructor("+argsList+");\n" +
"return __emval_register(obj);\n" +
"}\n";
/*jshint evil:true*/
return (new Function("requireRegisteredType", "HEAP32", "__emval_register", functionBody))(
requireRegisteredType, HEAP32, __emval_register);
}
function __emval_new(handle, argCount, argTypes, args) {
handle = requireHandle(handle);
var newer = __newers[argCount];
if (!newer) {
newer = craftEmvalAllocator(argCount);
__newers[argCount] = newer;
}
return newer(handle, argTypes, args);
}
// appease jshint (technically this code uses eval)
var global = (function(){return Function;})()('return this')();
function __emval_get_global(name) {
name = getStringOrSymbol(name);
return __emval_register(global[name]);
}
function __emval_get_module_property(name) {
name = getStringOrSymbol(name);
return __emval_register(Module[name]);
}
function __emval_get_property(handle, key) {
handle = requireHandle(handle);
key = requireHandle(key);
return __emval_register(handle[key]);
}
function __emval_set_property(handle, key, value) {
handle = requireHandle(handle);
key = requireHandle(key);
value = requireHandle(value);
handle[key] = value;
}
function __emval_as(handle, returnType, destructorsRef) {
handle = requireHandle(handle);
returnType = requireRegisteredType(returnType, 'emval::as');
var destructors = [];
var rd = __emval_register(destructors);
HEAP32[destructorsRef >> 2] = rd;
return returnType['toWireType'](destructors, handle);
}
function __emval_call(handle, argCount, argTypes, argv) {
handle = requireHandle(handle);
var types = lookupTypes(argCount, argTypes);
var args = new Array(argCount);
for (var i = 0; i < argCount; ++i) {
var type = types[i];
args[i] = type['readValueFromPointer'](argv);
argv += type.argPackAdvance;
}
var rv = handle.apply(undefined, args);
return __emval_register(rv);
}
function lookupTypes(argCount, argTypes, argWireTypes) {
var a = new Array(argCount);
for (var i = 0; i < argCount; ++i) {
a[i] = requireRegisteredType(
HEAP32[(argTypes >> 2) + i],
"parameter " + i);
}
return a;
}
function allocateDestructors(destructorsRef) {
var destructors = [];
HEAP32[destructorsRef >> 2] = __emval_register(destructors);
return destructors;
}
// Leave id 0 undefined. It's not a big deal, but might be confusing
// to have null be a valid method caller.
var methodCallers = [undefined];
function addMethodCaller(caller) {
var id = methodCallers.length;
methodCallers.push(caller);
return id;
}
function __emval_get_method_caller(argCount, argTypes) {
var types = lookupTypes(argCount, argTypes);
var retType = types[0];
var signatureName = retType.name + "_$" + types.slice(1).map(function (t) { return t.name; }).join("_") + "$";
var params = ["retType"];
var args = [retType];
var argsList = ""; // 'arg0, arg1, arg2, ... , argN'
for (var i = 0; i < argCount - 1; ++i) {
argsList += (i !== 0 ? ", " : "") + "arg" + i;
params.push("argType" + i);
args.push(types[1 + i]);
}
var functionBody =
"return function (handle, name, destructors, args) {\n";
for (var i = 0; i < argCount - 1; ++i) {
functionBody +=
" var arg" + i + " = argType" + i + ".readValueFromPointer(args);\n" +
" args += argType" + i + ".argPackAdvance;\n";
}
functionBody +=
" var rv = handle[name](" + argsList + ");\n" +
" return retType.toWireType(destructors, rv);\n" +
"};\n";
params.push(functionBody);
var invokerFunction = new_(Function, params).apply(null, args);
return addMethodCaller(createNamedFunction(signatureName, invokerFunction));
}
function __emval_call_method(caller, handle, methodName, destructorsRef, args) {
caller = methodCallers[caller];
handle = requireHandle(handle);
methodName = getStringOrSymbol(methodName);
return caller(handle, methodName, allocateDestructors(destructorsRef), args);
}
function __emval_has_function(handle, name) {
handle = requireHandle(handle);
name = getStringOrSymbol(name);
return handle[name] instanceof Function;
}
|