Example 1:
Code: Select all
PrototypeC.i Prototype_duk_create_heap(*alloc_func, *realloc_func, *free_func, *heap_udata, *fatal_handler)
PrototypeC Prototype_duk_destroy_heap(*ctx)
PrototypeC.i Prototype_duk_get_string(*ctx, idx.i)
PrototypeC.i Prototype_duk_push_string(*ctx, str.p-utf8)
PrototypeC.i Prototype_duk_push_c_function(*ctx, *func, nargs.i)
PrototypeC Prototype_duk_call(*ctx, nargs.i)
Global duk_create_heap.Prototype_duk_create_heap
Global duk_destroy_heap.Prototype_duk_destroy_heap
Global duk_get_string_.Prototype_duk_get_string
Global duk_push_string.Prototype_duk_push_string
Global duk_push_c_function.Prototype_duk_push_c_function
Global duk_call.Prototype_duk_call
Procedure.s duk_get_string(*ctx, idx.i)
Protected Result$, *String
*String = duk_get_string_(*ctx, idx.i)
If *String
Result$ = PeekS(*String, -1, #PB_UTF8)
EndIf
ProcedureReturn Result$
EndProcedure
Define.i duk_library
duk_library = OpenLibrary(#PB_Any, "libduktape.dll")
If Not duk_library
MessageRequester("Error", "Failed to open library", 0)
End
EndIf
duk_create_heap = GetFunction(duk_library, "duk_create_heap")
duk_destroy_heap = GetFunction(duk_library, "duk_destroy_heap")
duk_get_string_ = GetFunction(duk_library, "duk_get_string")
duk_push_string = GetFunction(duk_library, "duk_push_string")
duk_push_c_function = GetFunction(duk_library, "duk_push_c_function")
duk_call = GetFunction(duk_library, "duk_call")
ProcedureC.i show_text(*ctx)
Debug duk_get_string(*ctx, 0)
ProcedureReturn 0
EndProcedure
*ctx = duk_create_heap(#Null, #Null, #Null, #Null, #Null)
If *ctx
duk_push_c_function(*ctx, @show_text(), 1)
duk_push_string(*ctx, "Hello")
duk_call(*ctx, 1)
duk_destroy_heap(*ctx)
EndIf
Example 2:
Code: Select all
#DUK_COMPILE_EVAL = (1 << 3)
#DUK_COMPILE_NOSOURCE = (1 << 9)
#DUK_COMPILE_STRLEN = (1 << 10)
#DUK_COMPILE_NOFILENAME = (1 << 11)
PrototypeC.i Prototype_duk_create_heap(*alloc_func, *realloc_func, *free_func, *heap_udata, *fatal_handler)
PrototypeC Prototype_duk_destroy_heap(*ctx)
PrototypeC.i Prototype_duk_get_string(*ctx, idx.i)
PrototypeC.i Prototype_duk_push_string(*ctx, str.p-utf8)
PrototypeC.i Prototype_duk_push_c_function(*ctx, *func, nargs.i)
PrototypeC Prototype_duk_call(*ctx, nargs.i)
PrototypeC.i Prototype_duk_put_global_string(*ctx, key.p-utf8)
PrototypeC.i Prototype_duk_eval_raw(*ctx, *src_buffer, src_length.i, flags.i)
Global duk_create_heap.Prototype_duk_create_heap
Global duk_destroy_heap.Prototype_duk_destroy_heap
Global duk_get_string_.Prototype_duk_get_string
Global duk_push_string.Prototype_duk_push_string
Global duk_push_c_function.Prototype_duk_push_c_function
Global duk_call.Prototype_duk_call
Global duk_put_global_string.Prototype_duk_put_global_string
Global duk_eval_raw.Prototype_duk_eval_raw
Procedure duk_eval_string(*ctx, src.s)
Protected *src
*src = UTF8(src)
duk_eval_raw(*ctx, *src, 0, 0 | #DUK_COMPILE_EVAL | #DUK_COMPILE_NOSOURCE | #DUK_COMPILE_STRLEN | #DUK_COMPILE_NOFILENAME)
FreeMemory(*src)
EndProcedure
Procedure.s duk_get_string(*ctx, idx.i)
Protected Result$, *String
*String = duk_get_string_(*ctx, idx.i)
If *String
Result$ = PeekS(*String, -1, #PB_UTF8)
EndIf
ProcedureReturn Result$
EndProcedure
Define.i duk_library
duk_library = OpenLibrary(#PB_Any, "libduktape.dll")
If Not duk_library
MessageRequester("Error", "Failed to open library", 0)
End
EndIf
duk_create_heap = GetFunction(duk_library, "duk_create_heap")
duk_destroy_heap = GetFunction(duk_library, "duk_destroy_heap")
duk_get_string_ = GetFunction(duk_library, "duk_get_string")
duk_push_string = GetFunction(duk_library, "duk_push_string")
duk_push_c_function = GetFunction(duk_library, "duk_push_c_function")
duk_call = GetFunction(duk_library, "duk_call")
duk_put_global_string = GetFunction(duk_library, "duk_put_global_string")
duk_eval_raw = GetFunction(duk_library, "duk_eval_raw")
ProcedureC.i show_text(*ctx)
Debug duk_get_string(*ctx, 0)
ProcedureReturn 0
EndProcedure
*ctx = duk_create_heap(#Null, #Null, #Null, #Null, #Null)
duk_push_c_function(*ctx, @show_text(), 1)
duk_put_global_string(*ctx, "foo")
duk_eval_string(*ctx, "foo('Hello');")
duk_destroy_heap(*ctx)