UNPKG

2.21 kBJavaScriptView Raw
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'use strict';
16
17const Util = require('@accordproject/ergo-compiler').Util;
18const logger = require('@accordproject/ergo-compiler').Logger;
19const moment = require('moment-mini');
20// Make sure Moment serialization preserves utcOffset. See https://momentjs.com/docs/#/displaying/as-json/
21moment.fn.toJSON = Util.momentToJson;
22
23const 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 */
34class EvalEngine extends Engine {
35 /**
36 * Engine kind
37 * @return {string} which kind of engine
38 */
39 kind() {
40 return 'eval';
41 }
42
43 /**
44 * 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 * Execute a call in a JavaScript machine
54 * @param {number} utcOffset - UTC Offset for this execution
55 * @param {object} now - the definition of 'now'
56 * @param {object} options to the text generation
57 * @param {object} context - global variables to set in the VM
58 * @param {object} script - the initial script to load
59 * @param {object} call - the execution call
60 * @return {object} the result of execution
61 */
62 runVMScriptCall(utcOffset,now,options,context,script,call) {
63 logger.debug(`Calling eval with context ${context}`);
64 const response = eval(script + call);
65 return response;
66 }
67
68}
69
70module.exports = EvalEngine;