This repository has been archived by the owner on Feb 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
index.js
62 lines (57 loc) · 2.21 KB
/
index.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
function setupMethods (soljson){
var compileJSON = soljson.cwrap("compileJSON", "string", ["string", "number"]);
var compileJSONMulti =
'_compileJSONMulti' in soljson ?
soljson.cwrap("compileJSONMulti", "string", ["string", "number"]) :
null;
var compileJSONCallback = null;
if ('_compileJSONCallback' in soljson)
{
/// TODO: Allocating memory and copying the strings over
/// to the emscripten runtime does not seem to work.
var copyString = function(str, ptr) {
var buffer = soljson._malloc(str.length + 1);
soljson.writeStringToMemory(str, buffer);
soljson.setValue(ptr, buffer, '*');
};
var wrapCallback = function(callback) {
return soljson.Runtime.addFunction(function(path, contents, error) {
// path is char*, contents is char**, error is char**
// TODO copying the results does not seem to work.
// This is not too bad, because most of the requests
// cannot be answered synchronously anyway.
var result = callback(soljson.Pointer_stringify(path));
if (typeof(result.contents) === typeof(''))
copyString(result.contents, contents);
if (typeof(result.error) === typeof(''))
copyString(result.error, error);
});
};
var compileInternal = soljson.cwrap("compileJSONCallback", "string", ["string", "number", "number"]);
compileJSONCallback = function(input, optimize, readCallback) {
var cb = wrapCallback(readCallback);
var output = compileInternal(input, optimize, cb);
soljson.Runtime.removeFunction(cb);
return output;
};
}
var compile = function(input, optimise, readCallback) {
var result = '';
if (readCallback !== undefined && compileJSONCallback !== null)
result = compileJSONCallback(JSON.stringify(input), optimise, readCallback);
if (typeof(input) != typeof('') && compileJSONMulti !== null)
result = compileJSONMulti(JSON.stringify(input), optimise);
else
result = compileJSON(input, optimise);
return JSON.parse(result);
}
var version = soljson.cwrap("version", "string", []);
return {
version: version,
compile: compile,
useVersion: function( versionString ){
return setupMethods( require('./bin/soljson-' + versionString + '.js' ) );
}
}
}
module.exports = setupMethods( require('./bin/soljson-latest.js') );