/
coffee.coffee
67 lines (58 loc) · 1.65 KB
/
coffee.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
# RiveScript.js
#
# This code is released under the MIT License.
# See the "LICENSE" file for more information.
#
# http://www.rivescript.com/
"use strict"
coffee = require "coffee-script"
##
# CoffeeObjectHandler (RiveScript master)
#
# CoffeeScript Language Support for RiveScript Macros. This language is not
# enabled by default; to enable CoffeeScript object macros:
#
# ```coffeescript
# CoffeeObjectHandler = require "rivescript/lang/coffee"
# $bot->setHandler("coffee", CoffeeObjectHandler);
# ```
##
class CoffeeObjectHandler
constructor: (master) ->
@_master = master
@_objects = {}
##
# void load (string name, string[] code)
#
# Called by the RiveScript object to load CoffeeScript code.
##
load: (name, code) ->
# We need to make a dynamic CoffeeScript function.
source = "this._objects[\"" + name + "\"] = function(rs, args) {\n" \
+ coffee.compile(code.join("\n"), {bare: true}) \
+ "}\n"
try
eval source
catch e
@_master.warn "Error evaluating CoffeeScript object: " + e.message
##
# string call (RiveScript rs, string name, string[] fields)
#
# Called by the RiveScript object to execute CoffeeScript 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 CoffeeScript object: #{e.message}]"
# Allow undefined responses.
if reply is undefined
reply = ""
return reply
module.exports = CoffeeObjectHandler