Skip to content

Commit fce68b0

Browse files
committed
docs: interpreter instance methods
1 parent d9da7ca commit fce68b0

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

README.md

+45
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,51 @@ js.evaluate(script).then(function (res) {
8585
In the example above the second request is sent in parallel, without waiting for the response from the first request.
8686

8787

88+
## API
89+
90+
##### new JSONScript(Object options) -> Object
91+
92+
Create JSONScript interpreter instance.
93+
94+
95+
##### .validate(Any script) -> Boolean
96+
97+
Validate script. This method is called automatically before the script is evaluated.
98+
99+
100+
##### .evaluate(Any script, Any data) -&gt; Promise<Any>
101+
102+
Evaluate script. Returns Promise that resolves to the script evaluation result or rejects in case of validation or executor error.
103+
104+
105+
##### .addExecutor(String name, Function|Object executor)
106+
107+
Add executor to the interpreter. Can be an object or a function with methods.
108+
109+
110+
##### .addInstruction(Object definition, Function func)
111+
112+
Define JSONScript instruction. Core instructions are added using this method too.
113+
114+
`definition` should be valid according to the [instruction schema](http://www.json-script.com/schema/instruction.json#).
115+
116+
`func` is the function used to evaluate instruction, it can return:
117+
118+
- Promise that resolves to the evaluation result
119+
- instance of Script that can contain:
120+
- a script that should be evaluated
121+
- a Promise that resolves to a script (for delayed evaluation).
122+
123+
Class `Script` is available as the property of both the class and the instance of JSONScript interpreter.
124+
125+
126+
##### .addMacro(Object definition)
127+
128+
Define macro. Core macros are added using this method too.
129+
130+
`definition` should be valid according to the [macro schema](http://www.json-script.com/schema/macro.json#).
131+
132+
88133
## Language
89134

90135
See [JSONScript language documentation](https://github.com/JSONScript/jsonscript/blob/master/LANGUAGE.md) for more information.

lib/jsonscript.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ function JSONScript(opts) {
2323
// TODO use addExecutor method
2424
this._executors = util.copy(this._opts.executors);
2525
this._util = util;
26-
this.ajv = Ajv({ v5: true, jsonPointers: true, passContext: true });
27-
this.Script = Script;
26+
this.ajv = new Ajv({ v5: true, jsonPointers: true, passContext: true });
2827

2928
addAjvKeywords.call(this);
3029
addCoreInstructions.call(this);
@@ -40,6 +39,7 @@ JSONScript.prototype.evaluate = evaluateScript;
4039
JSONScript.prototype.addInstruction = addInstruction;
4140
JSONScript.prototype.addExecutor = addExecutor;
4241
JSONScript.prototype.addMacro = addMacro;
42+
JSONScript.prototype.Script = JSONScript.Script = Script;
4343

4444

4545
/**

0 commit comments

Comments
 (0)