aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChad Austin <caustin@gmail.com>2014-03-24 00:54:18 -0700
committerChad Austin <chad@chadaustin.me>2014-03-28 23:56:41 -0700
commitfa3e4f460a86ee2e0212fe2471690f67b9b4280c (patch)
tree8f29ee37885f8249bd9bea84bf5dadc5acf99048 /src
parentbcb2da768ab5b559b1603b28e3f7b1a3e1b0b6fc (diff)
Make val::call<> compatible with asm.js
Diffstat (limited to 'src')
-rw-r--r--src/embind/embind.js11
-rw-r--r--src/embind/emval.js49
2 files changed, 41 insertions, 19 deletions
diff --git a/src/embind/embind.js b/src/embind/embind.js
index 4ef8646f..1fb9403f 100644
--- a/src/embind/embind.js
+++ b/src/embind/embind.js
@@ -164,6 +164,10 @@ var typeDependencies = {};
var registeredPointers = {};
function registerType(rawType, registeredInstance) {
+ if (!('varArgAdvance' in registeredInstance)) {
+ throw new TypeError('registerType registeredInstance requires varArgAdvance');
+ }
+
var name = registeredInstance.name;
if (!rawType) {
throwBindingError('type "' + name + '" must have a positive integer typeid pointer');
@@ -268,6 +272,7 @@ function __embind_register_void(rawType, name) {
name = readLatin1String(name);
registerType(rawType, {
name: name,
+ 'varArgAdvance': 0,
'fromWireType': function() {
return undefined;
},
@@ -432,8 +437,10 @@ function __embind_register_float(rawType, name, size) {
},
'varArgAdvance': 8,
'readValueFromPointer': function(pointer) {
- var heap = (shift === 2) ? HEAPF32 : HEAPF64;
- return this['fromWireType'](heap[pointer >> shift]);
+ // TODO: rename readValueFromPointer to
+ // readValueFromVarArg in clang/emscripten, it appears
+ // floats are passed in varargs via HEAPF64
+ return this['fromWireType'](HEAPF64[pointer >> 3]);
},
writeValueToPointer: function(value, pointer, _destructors) {
var heap = (shift === 2) ? HEAPF32 : HEAPF64;
diff --git a/src/embind/emval.js b/src/embind/emval.js
index 535b5d57..3dfe0f00 100644
--- a/src/embind/emval.js
+++ b/src/embind/emval.js
@@ -228,40 +228,55 @@ function allocateDestructors(destructorsRef) {
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 args1 = ["addFunction", "createNamedFunction", "requireHandle", "getStringOrSymbol", "retType", "allocateDestructors"];
- var args2 = [Runtime.addFunction, createNamedFunction, requireHandle, getStringOrSymbol, retType, allocateDestructors];
+ var params = ["retType"];
+ var args = [retType];
var argsList = ""; // 'arg0, arg1, arg2, ... , argN'
- var argsListWired = ""; // 'arg0Wired, ..., argNWired'
for (var i = 0; i < argCount - 1; ++i) {
argsList += (i !== 0 ? ", " : "") + "arg" + i;
- argsListWired += ", arg" + i + "Wired";
- args1.push("argType" + i);
- args2.push(types[1 + i]);
+ params.push("argType" + i);
+ args.push(types[1 + i]);
}
- var invokerFnBody =
- "return addFunction(createNamedFunction('" + signatureName + "', function (handle, name, destructorsRef" + argsListWired + ") {\n" +
- " var handle = requireHandle(handle);\n" +
- " name = getStringOrSymbol(name);\n";
+ var functionBody =
+ "return function (handle, name, destructors, args) {\n";
for (var i = 0; i < argCount - 1; ++i) {
- invokerFnBody += " var arg" + i + " = argType" + i + ".fromWireType(arg" + i + "Wired);\n";
+ functionBody +=
+ " var arg" + i + " = argType" + i + ".readValueFromPointer(args);\n" +
+ " args += argType" + i + ".varArgAdvance;\n";
}
- invokerFnBody +=
+ functionBody +=
" var rv = handle[name](" + argsList + ");\n" +
- " return retType.toWireType(allocateDestructors(destructorsRef), rv);\n" +
- "}));\n";
+ " return retType.toWireType(destructors, rv);\n" +
+ "};\n";
- args1.push(invokerFnBody);
- var invokerFunction = new_(Function, args1).apply(null, args2);
- return invokerFunction;
+ 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) {