javascript

package
Version: v0.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 21, 2021 License: MIT Imports: 4 Imported by: 2

Documentation

Overview

Package javascript implements JavaScript object macros for RiveScript.

This is powered by the Otto JavaScript engine1, which is a JavaScript engine written in pure Go. It is not the V8 engine used by Node, so expect possible compatibility issues to arise.

Usage is simple. In your Golang code:

import (
	rivescript "github.com/aichaos/rivescript-go"
	"github.com/aichaos/rivescript-go/lang/javascript"
)

func main() {
	bot := rivescript.New(nil)
	jsHandler := javascript.New(bot)
	bot.SetHandler("javascript", jsHandler)

	// and go on as normal
}

And in your RiveScript code, you can load and run JavaScript objects:

> object add javascript
	var a = args[0];
	var b = args[1];
	return parseInt(a) + parseInt(b);
< object

> object setname javascript
	// Set the user's name via JavaScript
	var uid = rs.CurrentUser();
	rs.SetUservar(uid, args[0], args[1])
< object

+ add # and #
- <star1> + <star2> = <call>add <star1> <star2></call>

+ my name is *
- I will remember that.<call>setname <id> <formal></call>

+ what is my name
- You are <get name>.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type JavaScriptHandler

type JavaScriptHandler struct {
	// contains filtered or unexported fields
}

func New

func New(rs *rivescript.RiveScript) *JavaScriptHandler

New creates an object handler for JavaScript with its own Otto VM.

func (JavaScriptHandler) Call

func (js JavaScriptHandler) Call(name string, fields []string) string

Call executes a JavaScript macro and returns its results.

func (JavaScriptHandler) Load

func (js JavaScriptHandler) Load(name string, code []string)

Load loads a new JavaScript object macro into the VM.

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL