duktape

package module
v0.0.0-...-650f7c8 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2021 License: MIT Imports: 11 Imported by: 8

README

Duktape bindings for Go(Golang)

wercker status Travis status Appveyor status Gitter

Duktape is a thin, embeddable javascript engine. Most of the api is implemented. The exceptions are listed here.

Usage

The package is fully go-getable, no need to install any external C libraries.
So, just type go get gopkg.in/olebedev/go-duktape.v3 to install.

package main

import "fmt"
import "gopkg.in/olebedev/go-duktape.v3"

func main() {
  ctx := duktape.New()
  ctx.PevalString(`2 + 3`)
  result := ctx.GetNumber(-1)
  ctx.Pop()
  fmt.Println("result is:", result)
  // To prevent memory leaks, don't forget to clean up after
  // yourself when you're done using a context.
  ctx.DestroyHeap()
}
Go specific notes

Bindings between Go and Javascript contexts are not fully functional. However, binding a Go function to the Javascript context is available:

package main

import "fmt"
import "gopkg.in/olebedev/go-duktape.v3"

func main() {
  ctx := duktape.New()
  ctx.PushGlobalGoFunction("log", func(c *duktape.Context) int {
    fmt.Println(c.SafeToString(-1))
    return 0
  })
  ctx.PevalString(`log('Go lang Go!')`)
}

then run it.

$ go run *.go
Go lang Go!
$
Timers

There is a method to inject timers to the global scope:

package main

import "fmt"
import "gopkg.in/olebedev/go-duktape.v3"

func main() {
  ctx := duktape.New()

  // Let's inject `setTimeout`, `setInterval`, `clearTimeout`,
  // `clearInterval` into global scope.
  ctx.PushTimers()

  ch := make(chan string)
  ctx.PushGlobalGoFunction("second", func(_ *Context) int {
    ch <- "second step"
    return 0
  })
  ctx.PevalString(`
    setTimeout(second, 0);
    print('first step');
  `)
  fmt.Println(<-ch)
}

then run it

$ go run *.go
first step
second step
$

Also you can FlushTimers().

Command line tool

Install go get gopkg.in/olebedev/go-duktape.v3/....
Execute file.js: $GOPATH/bin/go-duk file.js.

Benchmarks
prog time
otto 200.13s
anko 231.19s
agora 149.33s
GopherLua 8.39s
go-duktape 9.80s

More details are here.

Status

The package is not fully tested, so be careful.

Contribution

Pull requests are welcome! Also, if you want to discuss something send a pull request with proposal and changes. Convention: fork the repository and make changes on your fork in a feature branch.

Documentation

Index

Examples

Constants

View Source
const (
	CompileEval       uint = C.DUK_COMPILE_EVAL
	CompileFunction   uint = C.DUK_COMPILE_FUNCTION
	CompileStrict     uint = C.DUK_COMPILE_STRICT
	CompileShebang    uint = C.DUK_COMPILE_SHEBANG
	CompileSafe       uint = C.DUK_COMPILE_SAFE
	CompileNoResult   uint = C.DUK_COMPILE_NORESULT
	CompileNoSource   uint = C.DUK_COMPILE_NOSOURCE
	CompileStrlen     uint = C.DUK_COMPILE_STRLEN
	CompileNoFileName uint = C.DUK_COMPILE_NOFILENAME
	CompileFuncExpr   uint = C.DUK_COMPILE_FUNCEXPR
)
View Source
const (
	TypeMaskNone      uint = C.DUK_TYPE_MASK_NONE
	TypeMaskUndefined uint = C.DUK_TYPE_MASK_UNDEFINED
	TypeMaskNull      uint = C.DUK_TYPE_MASK_NULL
	TypeMaskBoolean   uint = C.DUK_TYPE_MASK_BOOLEAN
	TypeMaskNumber    uint = C.DUK_TYPE_MASK_NUMBER
	TypeMaskString    uint = C.DUK_TYPE_MASK_STRING
	TypeMaskObject    uint = C.DUK_TYPE_MASK_OBJECT
	TypeMaskBuffer    uint = C.DUK_TYPE_MASK_BUFFER
	TypeMaskPointer   uint = C.DUK_TYPE_MASK_POINTER
	TypeMaskLightFunc uint = C.DUK_TYPE_MASK_LIGHTFUNC
)
View Source
const (
	EnumIncludeNonenumerable uint = C.DUK_ENUM_INCLUDE_NONENUMERABLE
	EnumIncludeHidden        uint = C.DUK_ENUM_INCLUDE_HIDDEN
	EnumIncludeSymbols       uint = C.DUK_ENUM_INCLUDE_SYMBOLS
	EnumExcludeStrings       uint = C.DUK_ENUM_EXCLUDE_STRINGS
	EnumOwnPropertiesOnly    uint = C.DUK_ENUM_OWN_PROPERTIES_ONLY
	EnumArrayIndicesOnly     uint = C.DUK_ENUM_ARRAY_INDICES_ONLY
	EnumSortArrayIndices     uint = C.DUK_ENUM_SORT_ARRAY_INDICES
	NoProxyBehavior          uint = C.DUK_ENUM_NO_PROXY_BEHAVIOR
)
View Source
const (
	ErrUnimplemented int = 50 + iota
	ErrUnsupported

	ErrNone      int = C.DUK_ERR_NONE
	ErrError     int = C.DUK_ERR_ERROR
	ErrEval      int = C.DUK_ERR_EVAL_ERROR
	ErrRange     int = C.DUK_ERR_RANGE_ERROR
	ErrReference int = C.DUK_ERR_REFERENCE_ERROR
	ErrSyntax    int = C.DUK_ERR_SYNTAX_ERROR
	ErrType      int = C.DUK_ERR_TYPE_ERROR
	ErrURI       int = C.DUK_ERR_URI_ERROR
)
View Source
const (
	// Returned error values
	ErrRetUnimplemented int = -(ErrUnimplemented + iota)
	ErrRetUnsupported
	ErrRetInternal
	ErrRetAlloc
	ErrRetAssertion
	ErrRetAPI
	ErrRetUncaughtError
)
View Source
const (
	ErrRetError     int = -(ErrError)
	ErrRetEval      int = -(ErrEval)
	ErrRetRange     int = -(ErrRange)
	ErrRetReference int = -(ErrReference)
	ErrRetSyntax    int = -(ErrSyntax)
	ErrRetType      int = -(ErrType)
	ErrRetURI       int = -(ErrURI)
)
View Source
const (
	ExecSuccess int = C.DUK_EXEC_SUCCESS
	ExecError   int = C.DUK_EXEC_ERROR
)
View Source
const (
	LogTrace int = iota
	LogDebug
	LogInfo
	LogWarn
	LogError
	LogFatal
)
View Source
const (
	BufObjArrayBuffer       int = C.DUK_BUFOBJ_ARRAYBUFFER
	BufObjNodejsBuffer      int = C.DUK_BUFOBJ_NODEJS_BUFFER
	BufObjDataView          int = C.DUK_BUFOBJ_DATAVIEW
	BufobjInt8Array         int = C.DUK_BUFOBJ_INT8ARRAY
	BufobjUint8Array        int = C.DUK_BUFOBJ_UINT8ARRAY
	BufobjUint8ClampedArray int = C.DUK_BUFOBJ_UINT8CLAMPEDARRAY
	BufObjInt16Array        int = C.DUK_BUFOBJ_INT16ARRAY
	BufObjUint16Array       int = C.DUK_BUFOBJ_UINT16ARRAY
	BufObjInt32Array        int = C.DUK_BUFOBJ_INT32ARRAY
	BufObjUint32Array       int = C.DUK_BUFOBJ_UINT32ARRAY
	BufObjFloat32Array      int = C.DUK_BUFOBJ_FLOAT32ARRAY
	BufObjFloat64Array      int = C.DUK_BUFOBJ_FLOAT64ARRAY
)
View Source
const FlagConsoleFlush = 1 << 1

FlagConsoleFlush is a Console flag. Flush output after every call.

View Source
const FlagConsoleProxyWrapper = 1 << 0

FlagConsoleProxyWrapper is a Console flag. Use a proxy wrapper to make undefined methods (console.foo()) no-ops.

Variables

View Source
var DukDebuggerMaxAttachments = 64 // arbitrary number of max 64 debugger attachments :)

Functions

This section is empty.

Types

type Context

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

func New

func New() *Context

New returns plain initialized duktape context object See: http://duktape.org/api.html#duk_create_heap_default

func NewWithFlags

func NewWithFlags(flags *Flags) *Context

NewWithFlags returns plain initialized duktape context object You can control the behaviour of duktape by setting flags. See: http://duktape.org/api.html#duk_create_heap_default

func (*Context) Alloc

func (d *Context) Alloc(size int) unsafe.Pointer

See: http://duktape.org/api.html#duk_alloc

func (*Context) AllocRaw

func (d *Context) AllocRaw(size int) unsafe.Pointer

See: http://duktape.org/api.html#duk_alloc_raw

func (*Context) Base64Decode

func (d *Context) Base64Decode(index int)

See: http://duktape.org/api.html#duk_base64_decode

func (*Context) Base64Encode

func (d *Context) Base64Encode(index int) string

See: http://duktape.org/api.html#duk_base64_encode

func (*Context) Call

func (d *Context) Call(nargs int)

See: http://duktape.org/api.html#duk_call

func (*Context) CallMethod

func (d *Context) CallMethod(nargs int)

See: http://duktape.org/api.html#duk_call_method

func (*Context) CallProp

func (d *Context) CallProp(objIndex int, nargs int)

See: http://duktape.org/api.html#duk_call_prop

func (*Context) CheckStack

func (d *Context) CheckStack(extra int) bool

See: http://duktape.org/api.html#duk_check_stack

func (*Context) CheckStackTop

func (d *Context) CheckStackTop(top int) bool

See: http://duktape.org/api.html#duk_check_stack_top

func (*Context) CheckType

func (d *Context) CheckType(index int, typ int) bool

See: http://duktape.org/api.html#duk_check_type

func (*Context) CheckTypeMask

func (d *Context) CheckTypeMask(index int, mask uint) bool

See: http://duktape.org/api.html#duk_check_type_mask

func (*Context) Compact

func (d *Context) Compact(objIndex int)

See: http://duktape.org/api.html#duk_compact

func (*Context) Compile

func (d *Context) Compile(flags uint)

See: http://duktape.org/api.html#duk_compile

func (*Context) CompileFile

func (d *Context) CompileFile(flags uint, path string)

See: http://duktape.org/api.html#duk_compile_file

func (*Context) CompileLstring

func (d *Context) CompileLstring(flags uint, src string, lenght int)

See: http://duktape.org/api.html#duk_compile_lstring

func (*Context) CompileLstringFilename

func (d *Context) CompileLstringFilename(flags uint, src string, lenght int)

See: http://duktape.org/api.html#duk_compile_lstring_filename

func (*Context) CompileString

func (d *Context) CompileString(flags uint, src string)

See: http://duktape.org/api.html#duk_compile_string

func (*Context) CompileStringFilename

func (d *Context) CompileStringFilename(flags uint, src string)

See: http://duktape.org/api.html#duk_compile_string_filename

func (*Context) Concat

func (d *Context) Concat(count int)

See: http://duktape.org/api.html#duk_concat

func (*Context) ConfigBuffer

func (d *Context) ConfigBuffer(bufferIdx int, buffer []byte)

See: http://duktape.org/api.html#duk_config_buffer

func (*Context) Copy

func (d *Context) Copy(fromIndex int, toIndex int)

See: http://duktape.org/api.html#duk_copy

func (*Context) DefProp

func (d *Context) DefProp(objIndex int, flags uint)

See: http://duktape.org/api.html#duk_def_prop

func (*Context) DelProp

func (d *Context) DelProp(objIndex int) bool

See: http://duktape.org/api.html#duk_del_prop

func (*Context) DelPropIndex

func (d *Context) DelPropIndex(objIndex int, arrIndex uint) bool

See: http://duktape.org/api.html#duk_del_prop_index

func (*Context) DelPropString

func (d *Context) DelPropString(objIndex int, key string) bool

See: http://duktape.org/api.html#duk_del_prop_string

func (*Context) Destroy

func (d *Context) Destroy()

Destroy destroy all the references to the functions and freed the pointers

func (*Context) DestroyHeap

func (d *Context) DestroyHeap()

See: http://duktape.org/api.html#duk_destroy_heap

func (*Context) DumpContextStderr

func (d *Context) DumpContextStderr()

See: http://duktape.org/api.html#duk_dump_context_stderr

func (*Context) DumpContextStdout

func (d *Context) DumpContextStdout()

See: http://duktape.org/api.html#duk_dump_context_stdout

func (*Context) DumpFunction

func (d *Context) DumpFunction()

See: http://duktape.org/api.html#duk_dump_function

func (*Context) Dup

func (d *Context) Dup(fromIndex int)

See: http://duktape.org/api.html#duk_dup

func (*Context) DupTop

func (d *Context) DupTop()

See: http://duktape.org/api.html#duk_dup_top

func (*Context) Enum

func (d *Context) Enum(objIndex int, enumFlags uint)

See: http://duktape.org/api.html#duk_enum

func (*Context) Equals

func (d *Context) Equals(index1 int, index2 int) bool

See: http://duktape.org/api.html#duk_equals

func (*Context) Error

func (d *Context) Error(errCode int, str string)

Error pushes a new Error object to the stack and throws it. This will call fmt.Sprint, forwarding arguments after the error code, to produce the Error's message.

See: http://duktape.org/api.html#duk_error

func (*Context) ErrorRaw

func (d *Context) ErrorRaw(errCode int, filename string, line int, errMsg string)

func (*Context) ErrorVa

func (d *Context) ErrorVa(errCode int, a ...interface{})

See: http://duktape.org/api.html#duk_error_va

func (*Context) Errorf

func (d *Context) Errorf(errCode int, format string, a ...interface{})

Errorf pushes a new Error object to the stack and throws it. This will call fmt.Sprintf, forwarding the format string and additional arguments, to produce the Error's message.

See: http://duktape.org/api.html#duk_error

func (*Context) Eval

func (d *Context) Eval()

See: http://duktape.org/api.html#duk_eval

func (*Context) EvalFile

func (d *Context) EvalFile(path string)

See: http://duktape.org/api.html#duk_eval_file

func (*Context) EvalFileNoresult

func (d *Context) EvalFileNoresult(path string)

See: http://duktape.org/api.html#duk_eval_file_noresult

func (*Context) EvalLstring

func (d *Context) EvalLstring(src string, lenght int)

See: http://duktape.org/api.html#duk_eval_lstring

func (*Context) EvalLstringNoresult

func (d *Context) EvalLstringNoresult(src string, lenght int)

See: http://duktape.org/api.html#duk_eval_lstring_noresult

func (*Context) EvalNoresult

func (d *Context) EvalNoresult()

See: http://duktape.org/api.html#duk_eval_noresult

func (*Context) EvalString

func (d *Context) EvalString(src string)

See: http://duktape.org/api.html#duk_eval_string

func (*Context) EvalStringNoresult

func (d *Context) EvalStringNoresult(src string)

See: http://duktape.org/api.html#duk_eval_string_noresult

func (*Context) Fatal

func (d *Context) Fatal(errCode int, errMsg string)

See: http://duktape.org/api.html#duk_fatal

func (*Context) FlushTimers

func (d *Context) FlushTimers()

func (*Context) Gc

func (d *Context) Gc(flags uint)

See: http://duktape.org/api.html#duk_gc

func (*Context) GetBoolean

func (d *Context) GetBoolean(index int) bool

See: http://duktape.org/api.html#duk_get_boolean

func (*Context) GetBuffer

func (d *Context) GetBuffer(index int) (rawPtr unsafe.Pointer, outSize uint)

See: http://duktape.org/api.html#duk_get_buffer

func (*Context) GetContext

func (d *Context) GetContext(index int) *Context

See: http://duktape.org/api.html#duk_get_context

func (*Context) GetCurrentMagic

func (d *Context) GetCurrentMagic() int

See: http://duktape.org/api.html#duk_get_current_magic

func (*Context) GetErrorCode

func (d *Context) GetErrorCode(index int) int

See: http://duktape.org/api.html#duk_get_error_code

func (*Context) GetFinalizer

func (d *Context) GetFinalizer(index int)

See: http://duktape.org/api.html#duk_get_finalizer

func (*Context) GetGlobalString

func (d *Context) GetGlobalString(key string) bool

See: http://duktape.org/api.html#duk_get_global_string

func (*Context) GetHeapptr

func (d *Context) GetHeapptr(index int) unsafe.Pointer

See: http://duktape.org/api.html#duk_get_heapptr

func (*Context) GetInt

func (d *Context) GetInt(index int) int

See: http://duktape.org/api.html#duk_get_int

func (*Context) GetLength

func (d *Context) GetLength(index int) int

See: http://duktape.org/api.html#duk_get_length

func (*Context) GetLstring

func (d *Context) GetLstring(index int) string

See: http://duktape.org/api.html#duk_get_lstring

func (*Context) GetMagic

func (d *Context) GetMagic(index int) int

See: http://duktape.org/api.html#duk_get_magic

func (*Context) GetNumber

func (d *Context) GetNumber(index int) float64

See: http://duktape.org/api.html#duk_get_number

func (*Context) GetPointer

func (d *Context) GetPointer(index int) unsafe.Pointer

See: http://duktape.org/api.html#duk_get_pointer

func (*Context) GetProp

func (d *Context) GetProp(objIndex int) bool

See: http://duktape.org/api.html#duk_get_prop

func (*Context) GetPropIndex

func (d *Context) GetPropIndex(objIndex int, arrIndex uint) bool

See: http://duktape.org/api.html#duk_get_prop_index

func (*Context) GetPropString

func (d *Context) GetPropString(objIndex int, key string) bool

See: http://duktape.org/api.html#duk_get_prop_string

func (*Context) GetPrototype

func (d *Context) GetPrototype(index int)

See: http://duktape.org/api.html#duk_get_prototype

func (*Context) GetString

func (d *Context) GetString(i int) string

See: http://duktape.org/api.html#duk_get_string

func (*Context) GetTop

func (d *Context) GetTop() int

See: http://duktape.org/api.html#duk_get_top

func (*Context) GetTopIndex

func (d *Context) GetTopIndex() int

See: http://duktape.org/api.html#duk_get_top_index

func (*Context) GetType

func (d *Context) GetType(index int) Type

See: http://duktape.org/api.html#duk_get_type

func (*Context) GetTypeMask

func (d *Context) GetTypeMask(index int) uint

See: http://duktape.org/api.html#duk_get_type_mask

func (*Context) GetUint

func (d *Context) GetUint(index int) uint

See: http://duktape.org/api.html#duk_get_uint

func (*Context) HasProp

func (d *Context) HasProp(objIndex int) bool

See: http://duktape.org/api.html#duk_has_prop

func (*Context) HasPropIndex

func (d *Context) HasPropIndex(objIndex int, arrIndex uint) bool

See: http://duktape.org/api.html#duk_has_prop_index

func (*Context) HasPropString

func (d *Context) HasPropString(objIndex int, key string) bool

See: http://duktape.org/api.html#duk_has_prop_string

func (*Context) HexDecode

func (d *Context) HexDecode(index int)

See: http://duktape.org/api.html#duk_hex_decode

func (*Context) HexEncode

func (d *Context) HexEncode(index int) string

See: http://duktape.org/api.html#duk_hex_encode

func (*Context) Insert

func (d *Context) Insert(toIndex int)

See: http://duktape.org/api.html#duk_insert

func (*Context) Instanceof

func (d *Context) Instanceof(idx1, idx2 int) bool

See: http://duktape.org/api.html#duk_instanceof

func (*Context) IsArray

func (d *Context) IsArray(index int) bool

See: http://duktape.org/api.html#duk_is_array

func (*Context) IsBoolean

func (d *Context) IsBoolean(index int) bool

See: http://duktape.org/api.html#duk_is_boolean

func (*Context) IsBoundFunction

func (d *Context) IsBoundFunction(index int) bool

See: http://duktape.org/api.html#duk_is_bound_function

func (*Context) IsBuffer

func (d *Context) IsBuffer(index int) bool

See: http://duktape.org/api.html#duk_is_buffer

func (*Context) IsCFunction

func (d *Context) IsCFunction(index int) bool

See: http://duktape.org/api.html#duk_is_c_function

func (*Context) IsCallable

func (d *Context) IsCallable(index int) bool

See: http://duktape.org/api.html#duk_is_callable

func (*Context) IsConstructorCall

func (d *Context) IsConstructorCall() bool

See: http://duktape.org/api.html#duk_is_constructor_call

func (*Context) IsDynamicBuffer

func (d *Context) IsDynamicBuffer(index int) bool

See: http://duktape.org/api.html#duk_is_dynamic_buffer

func (*Context) IsEcmascriptFunction

func (d *Context) IsEcmascriptFunction(index int) bool

See: http://duktape.org/api.html#duk_is_ecmascript_function

func (*Context) IsError

func (d *Context) IsError(index int) bool

See: http://duktape.org/api.html#duk_is_error

func (*Context) IsFixedBuffer

func (d *Context) IsFixedBuffer(index int) bool

See: http://duktape.org/api.html#duk_is_fixed_buffer

func (*Context) IsFunction

func (d *Context) IsFunction(index int) bool

See: http://duktape.org/api.html#duk_is_function

func (*Context) IsLightfunc

func (d *Context) IsLightfunc(index int) bool

See: http://duktape.org/api.html#duk_is_lightfunc

func (*Context) IsNan

func (d *Context) IsNan(index int) bool

See: http://duktape.org/api.html#duk_is_nan

func (*Context) IsNull

func (d *Context) IsNull(index int) bool

See: http://duktape.org/api.html#duk_is_null

func (*Context) IsNullOrUndefined

func (d *Context) IsNullOrUndefined(index int) bool

See: http://duktape.org/api.html#duk_is_null_or_undefined

func (*Context) IsNumber

func (d *Context) IsNumber(index int) bool

See: http://duktape.org/api.html#duk_is_number

func (*Context) IsObject

func (d *Context) IsObject(index int) bool

See: http://duktape.org/api.html#duk_is_object

func (*Context) IsObjectCoercible

func (d *Context) IsObjectCoercible(index int) bool

See: http://duktape.org/api.html#duk_is_object_coercible

func (*Context) IsPointer

func (d *Context) IsPointer(index int) bool

See: http://duktape.org/api.html#duk_is_pointer

func (*Context) IsPrimitive

func (d *Context) IsPrimitive(index int) bool

See: http://duktape.org/api.html#duk_is_primitive

func (*Context) IsStrictCall

func (d *Context) IsStrictCall() bool

See: http://duktape.org/api.html#duk_is_strict_call

func (*Context) IsString

func (d *Context) IsString(index int) bool

See: http://duktape.org/api.html#duk_is_string

func (*Context) IsThread

func (d *Context) IsThread(index int) bool

See: http://duktape.org/api.html#duk_is_thread

func (*Context) IsUndefined

func (d *Context) IsUndefined(index int) bool

See: http://duktape.org/api.html#duk_is_undefined

func (*Context) IsValidIndex

func (d *Context) IsValidIndex(index int) bool

See: http://duktape.org/api.html#duk_is_valid_index

func (*Context) Join

func (d *Context) Join(count int)

See: http://duktape.org/api.html#duk_join

func (*Context) JsonDecode

func (d *Context) JsonDecode(index int)

See: http://duktape.org/api.html#duk_json_decode

func (*Context) JsonEncode

func (d *Context) JsonEncode(index int) string

See: http://duktape.org/api.html#duk_json_encode

func (*Context) LoadFunction

func (d *Context) LoadFunction()

See: http://duktape.org/api.html#duk_load_function

Example
// Example of serialization duktape function to a byte slice; deserialization from byte slice to duktape
// function and eval in another duktape context.
//
// Usecase: get byte slice from duktape function (to possibly later save it to the file, send by network and etc.)
// Please, keep in mind restrictions for a deserialized duktape function from bytecode:
// https://github.com/svaarala/duktape/blob/master/doc/bytecode.rst
//
//Excerpts:
//	When to use bytecode dump/load
//		There are two main motivations for using bytecode dump/load:
//			-	Performance
//			-	Obfuscation

// Parenthesis is necessary.
js := "(function dump_from() { return 'It\\'s alive!'; })"

ctxSerialize := New()

// Compile js to duktape function and put it on the context stack
ctxSerialize.EvalLstring(js, len(js))

// Transmute duktape function on the top of the context stack into serializable duktape bytecode
ctxSerialize.DumpFunction()

// void *duk_get_buffer(duk_context *ctx, duk_idx_t idx, duk_size_t *out_size);
// typedef size_t duk_size_t;
// duk_size_t equivalent in Go is uint, however slice len is of a type int
// and I don't think that the limit of max(int) can be reached so for the sake of simplicity
// lets assume that bytecode size is always can be contained in int
rawmem, bufsize := ctxSerialize.GetBuffer(-1)
// Check for null is necessary because duk_get_buffer can return NULL.
if uintptr(rawmem) == uintptr(0) {
	log.Fatalf("Can't interpret bytecode dump as a valid, non-empty buffer.")
}
rawmemslice := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{Data: uintptr(rawmem), Len: int(bufsize), Cap: int(bufsize)}))

// Creating another slice for bytecode is necessary because rawmemslice pointing at memory that belongs to
// current context. That memory will be freed during execution of DestroyHeap().
bytecode := make([]byte, bufsize)
copy(bytecode, rawmemslice)

// To prevent memory leaks, don't forget to clean up after
// yourself when you're done using a context.
ctxSerialize.DestroyHeap()

ctxDeserialize := New()

//creating buffer on the context stack
rawmem = ctxDeserialize.PushBuffer(len(bytecode), false)
if uintptr(rawmem) == uintptr(0) {
	log.Fatalf("Can't push buffer to the context stack.")
}

//copying bytecode into the created buffer
rawmemslice = *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{Data: uintptr(rawmem), Len: len(bytecode), Cap: len(bytecode)}))
copy(rawmemslice, bytecode)

// Transmute duktape bytecode into duktape function
ctxDeserialize.LoadFunction()

// Call the function on top of a stack, example function doesn't have arguments.
ctxDeserialize.Call(0)

//example function return value is string
retval := ctxDeserialize.GetString(-1)

// To prevent memory leaks, don't forget to clean up after
// yourself when you're done using a context.
ctxDeserialize.DestroyHeap()

fmt.Println(retval)
Output:

It's alive!

func (*Context) Log

func (d *Context) Log(loglevel int, format string, value interface{})

See: http://duktape.org/api.html#duk_log

func (*Context) LogVa

func (d *Context) LogVa(logLevel int, format string, values ...interface{})

See: http://duktape.org/api.html#duk_log_va

func (*Context) Must

func (d *Context) Must() *Context

Must returns existing *Context or throw panic. It is highly recommended to use Must all the time.

func (*Context) New

func (d *Context) New(nargs int)

See: http://duktape.org/api.html#duk_new

func (*Context) Next

func (d *Context) Next(enumIndex int, getValue bool) bool

See: http://duktape.org/api.html#duk_next

func (*Context) NormalizeIndex

func (d *Context) NormalizeIndex(index int) int

See: http://duktape.org/api.html#duk_normalize_index

func (*Context) Pcall

func (d *Context) Pcall(nargs int) int

See: http://duktape.org/api.html#duk_pcall

func (*Context) PcallMethod

func (d *Context) PcallMethod(nargs int) int

See: http://duktape.org/api.html#duk_pcall_method

func (*Context) PcallProp

func (d *Context) PcallProp(objIndex int, nargs int) int

See: http://duktape.org/api.html#duk_pcall_prop

func (*Context) Pcompile

func (d *Context) Pcompile(flags uint) error

See: http://duktape.org/api.html#duk_pcompile

func (*Context) PcompileFile

func (d *Context) PcompileFile(flags uint, path string) error

See: http://duktape.org/api.html#duk_pcompile_file

func (*Context) PcompileLstring

func (d *Context) PcompileLstring(flags uint, src string, lenght int) error

See: http://duktape.org/api.html#duk_pcompile_lstring

func (*Context) PcompileLstringFilename

func (d *Context) PcompileLstringFilename(flags uint, src string, lenght int) error

See: http://duktape.org/api.html#duk_pcompile_lstring_filename

func (*Context) PcompileString

func (d *Context) PcompileString(flags uint, src string) error

See: http://duktape.org/api.html#duk_pcompile_string

func (*Context) PcompileStringFilename

func (d *Context) PcompileStringFilename(flags uint, src string) error

See: http://duktape.org/api.html#duk_pcompile_string_filename

func (*Context) Peval

func (d *Context) Peval() error

See: http://duktape.org/api.html#duk_peval

func (*Context) PevalFile

func (d *Context) PevalFile(path string) error

See: http://duktape.org/api.html#duk_peval_file

func (*Context) PevalFileNoresult

func (d *Context) PevalFileNoresult(path string) int

See: http://duktape.org/api.html#duk_peval_file_noresult

func (*Context) PevalLstring

func (d *Context) PevalLstring(src string, lenght int) error

See: http://duktape.org/api.html#duk_peval_lstring

func (*Context) PevalLstringNoresult

func (d *Context) PevalLstringNoresult(src string, lenght int) int

See: http://duktape.org/api.html#duk_peval_lstring_noresult

func (*Context) PevalNoresult

func (d *Context) PevalNoresult() int

See: http://duktape.org/api.html#duk_peval_noresult

func (*Context) PevalString

func (d *Context) PevalString(src string) error

See: http://duktape.org/api.html#duk_peval_string

func (*Context) PevalStringNoresult

func (d *Context) PevalStringNoresult(src string) int

See: http://duktape.org/api.html#duk_peval_string_noresult

func (*Context) Pnew

func (d *Context) Pnew(nargs int) error

See: http://duktape.org/api.html#duk_pnew

func (*Context) Pop

func (d *Context) Pop()

See: http://duktape.org/api.html#duk_pop

func (*Context) Pop2

func (d *Context) Pop2()

See: http://duktape.org/api.html#duk_pop_2

func (*Context) Pop3

func (d *Context) Pop3()

See: http://duktape.org/api.html#duk_pop_3

func (*Context) PopN

func (d *Context) PopN(count int)

See: http://duktape.org/api.html#duk_pop_n

func (*Context) PushArray

func (d *Context) PushArray() int

See: http://duktape.org/api.html#duk_push_array

func (*Context) PushBoolean

func (d *Context) PushBoolean(val bool)

See: http://duktape.org/api.html#duk_push_boolean

func (*Context) PushBuffer

func (d *Context) PushBuffer(size int, dynamic bool) unsafe.Pointer

See: http://duktape.org/api.html#duk_push_buffer

func (*Context) PushBufferObject

func (d *Context) PushBufferObject(bufferIdx, size, length int, flags uint)

See: http://duktape.org/api.html#duk_push_buffer_object

func (*Context) PushCFunction

func (d *Context) PushCFunction(fn *[0]byte, nargs int64) int

See: http://duktape.org/api.html#duk_push_c_function

func (*Context) PushCLightfunc

func (d *Context) PushCLightfunc(fn *[0]byte, nargs, length, magic int) int

See: http://duktape.org/api.html#duk_push_c_lightfunc

func (*Context) PushContextDump

func (d *Context) PushContextDump()

See: http://duktape.org/api.html#duk_push_context_dump

func (*Context) PushCurrentFunction

func (d *Context) PushCurrentFunction()

See: http://duktape.org/api.html#duk_push_current_function

func (*Context) PushCurrentThread

func (d *Context) PushCurrentThread()

See: http://duktape.org/api.html#duk_push_current_thread

func (*Context) PushDynamicBuffer

func (d *Context) PushDynamicBuffer(size int) unsafe.Pointer

See: http://duktape.org/api.html#duk_push_dynamic_buffer

func (*Context) PushErrorObject

func (d *Context) PushErrorObject(errCode int, format string, value interface{})

See: http://duktape.org/api.html#duk_push_error_object

func (*Context) PushErrorObjectVa

func (d *Context) PushErrorObjectVa(errCode int, format string, values ...interface{})

See: http://duktape.org/api.html#duk_push_error_object_va

func (*Context) PushExternalBuffer

func (d *Context) PushExternalBuffer()

See: http://duktape.org/api.html#duk_push_external_buffer

func (*Context) PushFalse

func (d *Context) PushFalse()

See: http://duktape.org/api.html#duk_push_false

func (*Context) PushFixedBuffer

func (d *Context) PushFixedBuffer(size int) unsafe.Pointer

See: http://duktape.org/api.html#duk_push_fixed_buffer

func (*Context) PushGlobalGoFunction

func (d *Context) PushGlobalGoFunction(name string, fn func(*Context) int) (int, error)

PushGlobalGoFunction push the given function into duktape global object Returns non-negative index (relative to stack bottom) of the pushed function also returns error if the function name is invalid

func (*Context) PushGlobalObject

func (d *Context) PushGlobalObject()

See: http://duktape.org/api.html#duk_push_global_object

func (*Context) PushGlobalStash

func (d *Context) PushGlobalStash()

See: http://duktape.org/api.html#duk_push_global_stash

func (*Context) PushGoFunction

func (d *Context) PushGoFunction(fn func(*Context) int) int

PushGoFunction push the given function into duktape stack, returns non-negative index (relative to stack bottom) of the pushed function

func (*Context) PushHeapStash

func (d *Context) PushHeapStash()

See: http://duktape.org/api.html#duk_push_heap_stash

func (*Context) PushHeapptr

func (d *Context) PushHeapptr(ptr unsafe.Pointer)

See: http://duktape.org/api.html#duk_push_heapptr

func (*Context) PushInt

func (d *Context) PushInt(val int)

See: http://duktape.org/api.html#duk_push_int

func (*Context) PushLstring

func (d *Context) PushLstring(str string, lenght int) string

See: http://duktape.org/api.html#duk_push_lstring

func (*Context) PushNan

func (d *Context) PushNan()

See: http://duktape.org/api.html#duk_push_nan

func (*Context) PushNull

func (d *Context) PushNull()

See: http://duktape.org/api.html#duk_push_null

func (*Context) PushNumber

func (d *Context) PushNumber(val float64)

See: http://duktape.org/api.html#duk_push_number

func (*Context) PushObject

func (d *Context) PushObject() int

See: http://duktape.org/api.html#duk_push_object

func (*Context) PushPointer

func (d *Context) PushPointer(p unsafe.Pointer)

See: http://duktape.org/api.html#duk_push_pointer

func (*Context) PushString

func (d *Context) PushString(str string) string

See: http://duktape.org/api.html#duk_push_string

func (*Context) PushStringFile

func (d *Context) PushStringFile(path string) string

See: http://duktape.org/api.html#duk_push_string_file

func (*Context) PushThis

func (d *Context) PushThis()

See: http://duktape.org/api.html#duk_push_this

func (*Context) PushThread

func (d *Context) PushThread() int

See: http://duktape.org/api.html#duk_push_thread

func (*Context) PushThreadNewGlobalenv

func (d *Context) PushThreadNewGlobalenv() int

See: http://duktape.org/api.html#duk_push_thread_new_globalenv

func (*Context) PushThreadStash

func (d *Context) PushThreadStash(targetCtx *Context)

See: http://duktape.org/api.html#duk_push_thread_stash

func (*Context) PushTimers

func (d *Context) PushTimers() error

DefineTimers defines `setTimeout`, `clearTimeout`, `setInterval`, `clearInterval` into global context.

func (*Context) PushTrue

func (d *Context) PushTrue()

See: http://duktape.org/api.html#duk_push_true

func (*Context) PushUint

func (d *Context) PushUint(val uint)

See: http://duktape.org/api.html#duk_push_uint

func (*Context) PushUndefined

func (d *Context) PushUndefined()

See: http://duktape.org/api.html#duk_push_undefined

func (*Context) PutGlobalString

func (d *Context) PutGlobalString(key string) bool

See: http://duktape.org/api.html#duk_put_global_string

func (*Context) PutProp

func (d *Context) PutProp(objIndex int) bool

See: http://duktape.org/api.html#duk_put_prop

func (*Context) PutPropIndex

func (d *Context) PutPropIndex(objIndex int, arrIndex uint) bool

See: http://duktape.org/api.html#duk_put_prop_index

func (*Context) PutPropString

func (d *Context) PutPropString(objIndex int, key string) bool

See: http://duktape.org/api.html#duk_put_prop_string

func (*Context) Remove

func (d *Context) Remove(index int)

See: http://duktape.org/api.html#duk_remove

func (*Context) Replace

func (d *Context) Replace(toIndex int)

See: http://duktape.org/api.html#duk_replace

func (*Context) RequireBoolean

func (d *Context) RequireBoolean(index int) bool

See: http://duktape.org/api.html#duk_require_boolean

func (*Context) RequireBuffer

func (d *Context) RequireBuffer(index int) (rawPtr unsafe.Pointer, outSize uint)

See: http://duktape.org/api.html#duk_require_buffer

func (*Context) RequireCallable

func (d *Context) RequireCallable(index int)

See: http://duktape.org/api.html#duk_require_callable

func (*Context) RequireContext

func (d *Context) RequireContext(index int) *Context

See: http://duktape.org/api.html#duk_require_context

func (*Context) RequireFunction

func (d *Context) RequireFunction(index int)

See: http://duktape.org/api.html#duk_require_function

func (*Context) RequireHeapptr

func (d *Context) RequireHeapptr(index int) unsafe.Pointer

See: http://duktape.org/api.html#duk_require_heapptr

func (*Context) RequireInt

func (d *Context) RequireInt(index int) int

See: http://duktape.org/api.html#duk_require_int

func (*Context) RequireLstring

func (d *Context) RequireLstring(index int) string

See: http://duktape.org/api.html#duk_require_lstring

func (*Context) RequireNormalizeIndex

func (d *Context) RequireNormalizeIndex(index int) int

See: http://duktape.org/api.html#duk_require_normalize_index

func (*Context) RequireNull

func (d *Context) RequireNull(index int)

See: http://duktape.org/api.html#duk_require_null

func (*Context) RequireNumber

func (d *Context) RequireNumber(index int) float64

See: http://duktape.org/api.html#duk_require_number

func (*Context) RequireObjectCoercible

func (d *Context) RequireObjectCoercible(index int)

See: http://duktape.org/api.html#duk_require_object_coercible

func (*Context) RequirePointer

func (d *Context) RequirePointer(index int) unsafe.Pointer

See: http://duktape.org/api.html#duk_require_pointer

func (*Context) RequireStack

func (d *Context) RequireStack(extra int)

See: http://duktape.org/api.html#duk_require_stack

func (*Context) RequireStackTop

func (d *Context) RequireStackTop(top int)

See: http://duktape.org/api.html#duk_require_stack_top

func (*Context) RequireString

func (d *Context) RequireString(index int) string

See: http://duktape.org/api.html#duk_require_string

func (*Context) RequireTopIndex

func (d *Context) RequireTopIndex() int

See: http://duktape.org/api.html#duk_require_top_index

func (*Context) RequireTypeMask

func (d *Context) RequireTypeMask(index int, mask uint)

See: http://duktape.org/api.html#duk_require_type_mask

func (*Context) RequireUint

func (d *Context) RequireUint(index int) uint

See: http://duktape.org/api.html#duk_require_uint

func (*Context) RequireUndefined

func (d *Context) RequireUndefined(index int)

See: http://duktape.org/api.html#duk_require_undefined

func (*Context) RequireValidIndex

func (d *Context) RequireValidIndex(index int)

See: http://duktape.org/api.html#duk_require_valid_index

func (*Context) ResizeBuffer

func (d *Context) ResizeBuffer(index int, newSize int) unsafe.Pointer

See: http://duktape.org/api.html#duk_resize_buffer

func (*Context) SafeCall

func (d *Context) SafeCall(fn, args *[0]byte, nargs, nrets int) int

See: http://duktape.org/api.html#duk_safe_call

func (*Context) SafeToLstring

func (d *Context) SafeToLstring(index int) string

See: http://duktape.org/api.html#duk_safe_to_lstring

func (*Context) SafeToString

func (d *Context) SafeToString(index int) string

See: http://duktape.org/api.html#duk_safe_to_string

func (*Context) SetFinalizer

func (d *Context) SetFinalizer(index int)

See: http://duktape.org/api.html#duk_set_finalizer

func (*Context) SetGlobalObject

func (d *Context) SetGlobalObject()

See: http://duktape.org/api.html#duk_set_global_object

func (*Context) SetMagic

func (d *Context) SetMagic(index int, magic int)

See: http://duktape.org/api.html#duk_set_magic

func (*Context) SetPrototype

func (d *Context) SetPrototype(index int)

See: http://duktape.org/api.html#duk_set_prototype

func (*Context) SetTop

func (d *Context) SetTop(index int)

See: http://duktape.org/api.html#duk_set_top

func (*Context) StrictEquals

func (d *Context) StrictEquals(index1 int, index2 int) bool

func (*Context) Substring

func (d *Context) Substring(index int, startCharOffset int, endCharOffset int)

See: http://duktape.org/api.html#duk_substring

func (*Context) Swap

func (d *Context) Swap(index1 int, index2 int)

See: http://duktape.org/api.html#duk_swap

func (*Context) SwapTop

func (d *Context) SwapTop(index int)

See: http://duktape.org/api.html#duk_swap_top

func (*Context) Throw

func (d *Context) Throw()

See: http://duktape.org/api.html#duk_throw

func (*Context) ToBoolean

func (d *Context) ToBoolean(index int) bool

See: http://duktape.org/api.html#duk_to_boolean

func (*Context) ToBuffer

func (d *Context) ToBuffer(index int) (rawPtr unsafe.Pointer, outSize uint)

See: http://duktape.org/api.html#duk_to_buffer

func (*Context) ToDefaultvalue

func (d *Context) ToDefaultvalue(index int, hint int)

See: http://duktape.org/api.html#duk_to_defaultvalue

func (*Context) ToDynamicBuffer

func (d *Context) ToDynamicBuffer(index int) (rawPtr unsafe.Pointer, outSize uint)

See: http://duktape.org/api.html#duk_to_dynamic_buffer

func (*Context) ToFixedBuffer

func (d *Context) ToFixedBuffer(index int) (rawPtr unsafe.Pointer, outSize uint)

See: http://duktape.org/api.html#duk_to_fixed_buffer

func (*Context) ToInt

func (d *Context) ToInt(index int) int

See: http://duktape.org/api.html#duk_to_int

func (*Context) ToInt32

func (d *Context) ToInt32(index int) int32

See: http://duktape.org/api.html#duk_to_int32

func (*Context) ToLstring

func (d *Context) ToLstring(index int) string

See: http://duktape.org/api.html#duk_to_lstring

func (*Context) ToNull

func (d *Context) ToNull(index int)

See: http://duktape.org/api.html#duk_to_null

func (*Context) ToNumber

func (d *Context) ToNumber(index int) float64

See: http://duktape.org/api.html#duk_to_number

func (*Context) ToObject

func (d *Context) ToObject(index int)

See: http://duktape.org/api.html#duk_to_object

func (*Context) ToPointer

func (d *Context) ToPointer(index int) unsafe.Pointer

See: http://duktape.org/api.html#duk_to_pointer

func (*Context) ToPrimitive

func (d *Context) ToPrimitive(index int, hint int)

See: http://duktape.org/api.html#duk_to_primitive

func (*Context) ToString

func (d *Context) ToString(index int) string

See: http://duktape.org/api.html#duk_to_string

func (*Context) ToUint

func (d *Context) ToUint(index int) uint

See: http://duktape.org/api.html#duk_to_uint

func (*Context) ToUint16

func (d *Context) ToUint16(index int) uint16

See: http://duktape.org/api.html#duk_to_uint16

func (*Context) ToUint32

func (d *Context) ToUint32(index int) uint32

See: http://duktape.org/api.html#duk_to_uint32

func (*Context) ToUndefined

func (d *Context) ToUndefined(index int)

See: http://duktape.org/api.html#duk_to_undefined

func (*Context) Trim

func (d *Context) Trim(index int)

See: http://duktape.org/api.html#duk_trim

func (*Context) XcopyTop

func (d *Context) XcopyTop(fromCtx *Context, count int)

See: http://duktape.org/api.html#duk_xcopy_top

func (*Context) XmoveTop

func (d *Context) XmoveTop(fromCtx *Context, count int)

See: http://duktape.org/api.html#duk_xmove_top

type DebugDetachedFunc

type DebugDetachedFunc = func(ctx *Context, uData unsafe.Pointer)

type DebugNotifyFunc

type DebugNotifyFunc = func(ctx *Context) int

type DebugPeekFunc

type DebugPeekFunc = func(uData unsafe.Pointer) uint

type DebugReadFlushFunc

type DebugReadFlushFunc = func(uData unsafe.Pointer)

type DebugReadFunc

type DebugReadFunc = func(uData unsafe.Pointer, buffer []byte) uint

type DebugRequestFunc

type DebugRequestFunc = func(ctx *Context, uData unsafe.Pointer, nValues int) int

type DebugWriteFlushFunc

type DebugWriteFlushFunc = func(uData unsafe.Pointer)

type DebugWriteFunc

type DebugWriteFunc = func(uData unsafe.Pointer, buffer []byte) uint

type Debugger

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

func DukDebugger

func DukDebugger() *Debugger

Returns the Duktape debugger instance which can be attached to multiple Duktape contexts using the Attach method

func (*Debugger) Attach

func (d *Debugger) Attach(ctx *Context,
	readFunc DebugReadFunc,
	writeFunc DebugWriteFunc,
	peekFunc DebugPeekFunc,
	readFlushFunc DebugReadFlushFunc,
	writeFlushFunc DebugWriteFlushFunc,
	requestFunc DebugRequestFunc,
	detachedFunc DebugDetachedFunc,
	uData interface{}) error

See: http://duktape.org/api.html#duk_debugger_attach

All parameters are optional, except for readFunc, writeFunc.

func (*Debugger) Cooperate

func (d *Debugger) Cooperate(ctx *Context)

See: http://duktape.org/api.html#duk_debugger_cooperate

func (*Debugger) Detach

func (d *Debugger) Detach(ctx *Context)

See: http://duktape.org/api.html#duk_debugger_detach

func (*Debugger) Notify

func (d *Debugger) Notify(ctx *Context, notifyFunc DebugNotifyFunc) int

See: http://duktape.org/api.html#duk_debugger_notify

func (*Debugger) Pause

func (d *Debugger) Pause(ctx *Context)

See: http://duktape.org/api.html#duk_debugger_pause

type Error

type Error struct {
	Type       string
	Message    string
	FileName   string
	LineNumber int
	Stack      string
}

func (*Error) Error

func (e *Error) Error() string

type Flags

type Flags struct {
	Logging    uint
	PrintAlert uint
	Console    uint
}

Flags is a set of flags for controlling the behaviour of duktape.

type SocketTransport

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

func NewSocketTransport

func NewSocketTransport(ctx *Context,
	network, addr string,
	requestFunc DebugRequestFunc,
	detachedFunc DebugDetachedFunc,
	uData interface{}) (*SocketTransport, error)

func (*SocketTransport) Close

func (s *SocketTransport) Close() error

func (*SocketTransport) Listen

func (s *SocketTransport) Listen(errorListener func(err error))

type Type

type Type uint
const (
	TypeNone      Type = C.DUK_TYPE_NONE
	TypeUndefined Type = C.DUK_TYPE_UNDEFINED
	TypeNull      Type = C.DUK_TYPE_NULL
	TypeBoolean   Type = C.DUK_TYPE_BOOLEAN
	TypeNumber    Type = C.DUK_TYPE_NUMBER
	TypeString    Type = C.DUK_TYPE_STRING
	TypeObject    Type = C.DUK_TYPE_OBJECT
	TypeBuffer    Type = C.DUK_TYPE_BUFFER
	TypePointer   Type = C.DUK_TYPE_POINTER
	TypeLightFunc Type = C.DUK_TYPE_LIGHTFUNC
)

func (Type) IsBool

func (t Type) IsBool() bool

func (Type) IsBuffer

func (t Type) IsBuffer() bool

func (Type) IsLightFunc

func (t Type) IsLightFunc() bool

func (Type) IsNone

func (t Type) IsNone() bool

func (Type) IsNull

func (t Type) IsNull() bool

func (Type) IsNumber

func (t Type) IsNumber() bool

func (Type) IsObject

func (t Type) IsObject() bool

func (Type) IsPointer

func (t Type) IsPointer() bool

func (Type) IsString

func (t Type) IsString() bool

func (Type) IsUndefined

func (t Type) IsUndefined() bool

func (Type) String

func (t Type) String() string

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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