-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
118 lines (93 loc) · 3.98 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
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
var { ErynEngine } = require('./build-load')(__dirname);
const v8 = require('v8');
function bridgeDeepClone(obj) {
return v8.deserialize(v8.serialize(obj));
}
function bridgeShallowClone(obj) {
return Object.assign({}, obj);
}
function bridgeEval(script, context, local, shared) {
return eval(script);
}
class ErynBinding {
constructor(options) {
if (!options) {
options = {};
}
this.binding = new ErynEngine(options);
this.bridgeOptions = {
enableDeepCloning: false
};
// Annoying edge case.
if(options.hasOwnProperty("enableDeepCloning") && ((typeof options.enableDeepCloning) === "boolean")) {
this.bridgeOptions.enableDeepCloning = options.enableDeepCloning;
}
}
compile(path) {
if(!(path && (typeof path === 'string' && !(path instanceof String))))
throw `Invalid argument 'path' (expected: string | found: ${typeof(path)})`
this.binding.compile(path);
}
compileDir(dirPath, filters) {
if(!dirPath)
dirPath = "";
if(!(typeof dirPath === 'string' || (dirPath instanceof String)))
throw `Invalid argument 'dirPath' (expected: string | found: ${typeof(dirPath)})`
if(!(filters && (filters instanceof Array)))
throw `Invalid argument 'filters' (expected: array | found: ${typeof(filters)})`
this.binding.compileDir(dirPath, filters);
}
compileString(alias, str) {
if(!(alias && (typeof alias === 'string' && !(alias instanceof String))))
throw `Invalid argument 'alias' (expected: string | found: ${typeof(path)})`
if(!(str && (typeof str === 'string' && !(str instanceof String))))
throw `Invalid argument 'str' (expected: string | found: ${typeof(path)})`
this.binding.compileString(alias, str);
}
express(path, context, callback) {
try {
callback(null, this.binding.render(path, context));
} catch (error) {
callback(error, null);
}
}
render(path, context, shared) {
if(!(path && (typeof path === 'string' && !(path instanceof String))))
throw `Invalid argument 'path' (expected: string | found: ${typeof(path)})`
if(!context)
context = {};
if(!shared)
shared = {};
return this.binding.render(path, context, {}, shared, bridgeEval, this.bridgeOptions.enableDeepCloning ? bridgeDeepClone : bridgeShallowClone);
}
renderString(alias, context, shared) {
if(!(alias && (typeof alias === 'string' && !(alias instanceof String))))
throw `Invalid argument 'alias' (expected: string | found: ${typeof(alias)})`
if(!context)
context = {};
if(!shared)
shared = {};
return this.binding.renderString(alias, context, {}, shared, bridgeEval, this.bridgeOptions.enableDeepCloning ? bridgeDeepClone : bridgeShallowClone);
}
renderStringUncached(src, context, shared) {
if(!(src && (typeof src === 'string' && !(src instanceof String))))
throw `Invalid argument 'src' (expected: string | found: ${typeof(src)})`
if(!context)
context = {};
if(!shared)
shared = {};
this.compileString('__ERYN_uncached', src);
return this.binding.renderString('__ERYN_uncached', context, {}, shared, bridgeEval, this.bridgeOptions.enableDeepCloning ? bridgeDeepClone : bridgeShallowClone);
}
setOptions(options) {
if(!(options && (typeof options === 'object')))
throw `Invalid argument 'options' (expected: object | found: ${typeof(options)})`
if(options.hasOwnProperty("enableDeepCloning") && ((typeof options.enableDeepCloning) === "boolean"))
this.bridgeOptions.enableDeepCloning = options.enableDeepCloning;
this.binding.options(options);
}
}
const eryn = (options) => {
return new ErynBinding(options);
};
module.exports = eryn;