Skip to content
This repository has been archived by the owner on Oct 29, 2021. It is now read-only.

Add wrapper for duk_get_buffer_data #66

Open
wants to merge 2 commits into
base: v3
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion api.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,12 @@ func (d *Context) GetBuffer(index int) (rawPtr unsafe.Pointer, outSize uint) {
return rawPtr, outSize
}

// See: http://duktape.org/api.html#duk_get_buffer_data
func (d *Context) GetBufferData(index int) (rawPtr unsafe.Pointer, outSize uint) {
rawPtr = C.duk_get_buffer_data(d.duk_context, C.duk_idx_t(index), (*C.duk_size_t)(unsafe.Pointer(&outSize)))
return rawPtr, outSize
}

// See: http://duktape.org/api.html#duk_get_context
func (d *Context) GetContext(index int) *Context {
return contextFromPointer(C.duk_get_context(d.duk_context, C.duk_idx_t(index)))
Expand Down Expand Up @@ -629,6 +635,11 @@ func (d *Context) IsBuffer(index int) bool {
return int(C.duk_is_buffer(d.duk_context, C.duk_idx_t(index))) == 1
}

// See: http://duktape.org/api.html#duk_is_buffer_data
func (d *Context) IsBufferData(index int) bool {
return int(C.duk_is_buffer_data(d.duk_context, C.duk_idx_t(index))) == 1
}

// See: http://duktape.org/api.html#duk_is_c_function
func (d *Context) IsCFunction(index int) bool {
return int(C.duk_is_c_function(d.duk_context, C.duk_idx_t(index))) == 1
Expand Down Expand Up @@ -1308,6 +1319,33 @@ func (d *Context) SafeToString(index int) string {
return ""
}

func (c *Context) SafeToBytes(index int) []byte {
var input []byte = nil
switch c.GetType(index) {
case TypeString:
input = []byte(c.SafeToString(index))
break
case TypeBuffer:
inputPtr, inputLength := c.GetBuffer(index)
input = (*[1 << 30]byte)(inputPtr)[:inputLength:inputLength]
case TypeObject:
if c.IsBufferData(index) {
inputPtr, inputLength := c.GetBufferData(index)
input = (*[1 << 30]byte)(inputPtr)[:inputLength:inputLength]
} else {
fmt.Printf("cannot handle TypeObject content type of input param when SafeToBytes\n")
return nil
}
case TypePointer:
fmt.Printf("cannot handle TypePointer content type of input param when SafeToBytes\n")
return nil
default:
fmt.Printf("cannot guess content type of input param when SafeToBytes\n")
return nil
}
return input
}

// See: http://duktape.org/api.html#duk_set_finalizer
func (d *Context) SetFinalizer(index int) {
C.duk_set_finalizer(d.duk_context, C.duk_idx_t(index))
Expand Down Expand Up @@ -1584,7 +1622,6 @@ func (d *Context) ConfigBuffer(bufferIdx int, buffer []byte) {
* Realloc see: http://duktape.org/api.html#duk_realloc
* ReallocRaw see: http://duktape.org/api.html#duk_realloc_raw
* RequireCFunction see: http://duktape.org/api.html#duk_require_c_function
* GetBufferData see: http://duktape.org/api.html#duk_get_buffer_data
* StealBuffer see: http://duktape.org/api.html#duk_steal_buffer
* RequireBufferData see: http://duktape.org/api.html#duk_require_buffer_data
* IsEvalError see: http://duktape.org/api.html#duk_is_eval_error
Expand Down