1 | /*
|
2 | * Licensed under the Apache License, Version 2.0 (the "License");
|
3 | * you may not use this file except in compliance with the License.
|
4 | * You may obtain a copy of the License at
|
5 | *
|
6 | * http://www.apache.org/licenses/LICENSE-2.0
|
7 | *
|
8 | * Unless required by applicable law or agreed to in writing, software
|
9 | * distributed under the License is distributed on an "AS IS" BASIS,
|
10 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
11 | * See the License for the specific language governing permissions and
|
12 | * limitations under the License.
|
13 | */
|
14 |
|
15 | ;
|
16 |
|
17 | const Util = require('@accordproject/ergo-compiler').Util;
|
18 | const logger = require('@accordproject/ergo-compiler').Logger;
|
19 | const moment = require('moment-mini');
|
20 | // Make sure Moment serialization preserves utcOffset. See https://momentjs.com/docs/#/displaying/as-json/
|
21 | moment.fn.toJSON = Util.momentToJson;
|
22 |
|
23 | const Engine = require('./engine');
|
24 |
|
25 | /**
|
26 | * <p>
|
27 | * EvalEngine class. Execution of template logic against a request object, returning a response to the caller.
|
28 | * This is the eval-based engine.
|
29 | * </p>
|
30 | * @class
|
31 | * @public
|
32 | * @memberof module:ergo-engine
|
33 | */
|
34 | class EvalEngine extends Engine {
|
35 | /**
|
36 | * Engine kind
|
37 | * @return {string} which kind of engine
|
38 | */
|
39 | kind() {
|
40 | return 'eval';
|
41 | }
|
42 |
|
43 | /**
|
44 | * Call to compile a script for a JavaScript machine
|
45 | * @param {string} script - the script
|
46 | * @return {object} the VM-ready script object
|
47 | */
|
48 | compileVMScript(script) {
|
49 | return script;
|
50 | }
|
51 |
|
52 | /**
|
53 | * Call to execute a call in a JavaScript machine
|
54 | * @param {number} utcOffset - UTC Offset for this execution
|
55 | * @param {object} context - global variables to set in the VM
|
56 | * @param {object} script - the initial script to load
|
57 | * @param {object} call - the execution call
|
58 | * @return {object} the result of execution
|
59 | */
|
60 | runVMScriptCall(utcOffset,context,script,call) {
|
61 | logger.debug(`Calling eval with context ${context}`);
|
62 | const response = eval(script + call);
|
63 | return response;
|
64 | }
|
65 |
|
66 | }
|
67 |
|
68 | module.exports = EvalEngine;
|