Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: lib/compiledTools.js

Issue 29333474: Issue 4125 - [emscripten] Convert filter classes to C++ (Closed)
Patch Set: Created Jan. 14, 2016, 4:40 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « compiled/tools.cpp ('k') | lib/filterClassesNew.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/compiledTools.js
===================================================================
new file mode 100644
--- /dev/null
+++ b/lib/compiledTools.js
@@ -0,0 +1,73 @@
+"use strict";
+
+let {
+ Runtime, AsciiToString, HEAP16, HEAP32, _free, _getException
+} = require("./compiled");
+
+function wrapCall(call)
+{
+ return function()
+ {
+ let sp = Runtime.stackSave();
+ try
+ {
+ return call.apply(this, arguments);
+ }
+ catch (e)
+ {
+ throw typeof e == "number" ? new Error(AsciiToString(_getException(e))) : e;
+ }
+ finally
+ {
+ Runtime.stackRestore(sp);
+ }
+ };
+}
+exports.wrapCall = wrapCall;
+
+function convertString(str)
+{
+ let length = str.length;
+ let buffer = Runtime.stackAlloc(length * 2);
+ for (let i = 0, pointer = (buffer >> 1); i < length; i++, pointer++)
+ HEAP16[pointer] = str.charCodeAt(i);
+ return buffer;
+}
+
+function stringArg(argNumber, call)
+{
+ return function()
+ {
+ let params = Array.slice(arguments);
+ let str = params[argNumber];
+ if (str != null)
+ params.splice(argNumber, 1, convertString(str), str.length);
+ else
+ params.splice(argNumber, 1, 0, 0);
+ return call.apply(this, params);
+ };
+}
+exports.stringArg = stringArg;
+
+function stringResult(call)
+{
+ return function()
+ {
+ let params = Array.slice(arguments);
+ let resultLen = Runtime.stackAlloc(4);
+ params.push(resultLen);
+
+ let buffer = call.apply(this, params);
+ if (buffer)
+ {
+ let length = HEAP32[resultLen >> 2];
+ let pointer = buffer >> 1;
+ let str = String.fromCharCode.apply(String, HEAP16.slice(pointer, pointer + length));
+ _free(buffer);
+ return str;
+ }
+ else
+ return null;
+ };
+}
+exports.stringResult = stringResult;
« no previous file with comments | « compiled/tools.cpp ('k') | lib/filterClassesNew.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld