aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2012-02-23 10:16:01 -0800
committerAlon Zakai <alonzakai@gmail.com>2012-02-23 10:16:01 -0800
commit1710f5d258f7bc73477e5ccdc42a1e792504625a (patch)
treefa91c1aa9045f02df2476634e3ea7c1353a24754 /src
parentd64b12dde5154bee81f38bab766dd9cac837b2c6 (diff)
cwrap (like ccall, but returns a wrapper function)
Diffstat (limited to 'src')
-rw-r--r--src/preamble.js14
1 files changed, 14 insertions, 0 deletions
diff --git a/src/preamble.js b/src/preamble.js
index fce00fe2..6c29eade 100644
--- a/src/preamble.js
+++ b/src/preamble.js
@@ -414,6 +414,20 @@ function ccall(ident, returnType, argTypes, args) {
}
Module["ccall"] = ccall;
+// Returns a native JS wrapper for a C function. This is similar to ccall, but
+// returns a function you can call repeatedly in a normal way. For example:
+//
+// var my_function = cwrap('my_c_function', 'int', ['int', 'int']);
+// alert(my_function(5, 22));
+// alert(my_function(99, 12));
+//
+function cwrap(ident, returnType, argTypes) {
+ // TODO: optimize this, eval the whole function once instead of going through ccall each time
+ return function() {
+ return ccall(ident, returnType, argTypes, Array.prototype.slice.call(arguments));
+ }
+}
+
// Sets a value in memory in a dynamic way at run-time. Uses the
// type data. This is the same as makeSetValue, except that
// makeSetValue is done at compile-time and generates the needed