/
javascript.coffee
69 lines (61 loc) · 1.7 KB
/
javascript.coffee
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
# RiveScript.js
#
# This code is released under the MIT License.
# See the "LICENSE" file for more information.
#
# http://www.rivescript.com/
"use strict"
##
# JSObjectHandler (RiveScript master)
#
# JavaScript Language Support for RiveScript Macros. This support is enabled by
# default in RiveScript.js; if you don't want it, override the `javascript`
# language handler to null, like so:
#
# ```javascript
# $bot->setHandler("javascript", null);
# ```
##
class JSObjectHandler
constructor: (master) ->
@_master = master
@_objects = {}
##
# void load (string name, string[]|function code)
#
# Called by the RiveScript object to load JavaScript code.
##
load: (name, code) ->
if typeof code is "function"
# If code is just a js function, store the reference as is
@_objects[name] = code
else
# We need to make a dynamic JavaScript function.
source = "this._objects[\"" + name + "\"] = function(rs, args) {\n" \
+ code.join("\n") \
+ "}\n"
try
eval source
catch e
@_master.warn "Error evaluating JavaScript object: " + e.message
##
# string call (RiveScript rs, string name, string[] fields)
#
# Called by the RiveScript object to execute JavaScript code.
##
call: (rs, name, fields, scope) ->
# We have it?
if not @_objects[name]
return @_master.errors.objectNotFound
# Call the dynamic method.
func = @_objects[name]
reply = ""
try
reply = func.call(scope, rs, fields)
catch e
reply = "[ERR: Error when executing JavaScript object: #{e.message}]"
# Allow undefined responses.
if reply is undefined
reply = ""
return reply
module.exports = JSObjectHandler