Oxygen Engine
Modern C++ 3D Engine using OpenGL
Loading...
Searching...
No Matches
oe::ext::js::Runtime Struct Reference

JavaScript runtime. More...

#include <OxygenEngine/ext/javascript.h>

Public Member Functions

void addClass (CallableEntity class_info, const std::vector< CallableEntity > &methods)
 Add a class type available in JavaScript code.
 
void addFunction (CallableEntity function_info)
 add a C function available in in JavaScript code
 
void eval (const char *data)
 Evaluate a script in the current runtime.
 

Static Public Member Functions

static void pushJson (duk_context *ctx, const nlohmann::json &data, bool is_object=true)
 push a nlhomann::json object as a json to the runtime
 
static nlohmann::json getJson (duk_context *ctx, int index)
 pull a nlhomann::json object from a parameter
 

Public Attributes

duk_context * ctx
 

Detailed Description

JavaScript runtime.

Can bind function calls to C functions

Usage:

auto* ctx = js_runtime.ctx;
js_runtime.addClass(...);
js_runtime.addFunction(...);
js_runtime.eval(YOUR_SCRIPT_CODE);
// Call the function 'test' defined in JS, taking no arguments and returns a string
// Pushes "[global].test" into the stack
duk_push_global_object(ctx);
duk_get_prop_string(ctx, -1, "test");
// Call it (with 0 arguments)
duk_pcall(ctx, 0);
printf("test result is: %s\n", duk_safe_to_string(ctx, -1));
JavaScript runtime.
Definition javascript.h:94
void addFunction(CallableEntity function_info)
add a C function available in in JavaScript code
Definition javascript.h:130
void eval(const char *data)
Evaluate a script in the current runtime.
Definition javascript.h:206
void addClass(CallableEntity class_info, const std::vector< CallableEntity > &methods)
Add a class type available in JavaScript code.
Definition javascript.h:110

Here is another example where a class is defined and used as first argument:

// ... code from previous example ...
// Define a 'MyObject' class (callbacks are the same as the [Duktape tutorial about constructors](https://wiki.duktape.org/howtonativeconstructor))
js_runtime.addClass(
{
.name = "MyObject",
.callback = myobject_constructor,
.callback_args_count = 1
},
{
{
.name = "printName",
.callback = myobject_print_name,
.callback_args_count = 1
}
});
// Call the function 'test2' defined in JS, taking 1 argument of type MyObject (defined before) and returns a string
// Pushes "[global].test2" into the stack
duk_get_global_string(ctx, "test2");
// Create an instance of MyObject with a member value of gabuzomeu
duk_get_global_string(ctx, "MyObject");
duk_push_string(ctx, "gabuzomeu");
duk_new(ctx, 1);
// Call it (with 1 arguments)
duk_pcall(ctx, 1);
printf("test2 result is: %s\n", duk_safe_to_string(ctx, -1));

Member Function Documentation

◆ addClass()

void oe::ext::js::Runtime::addClass ( CallableEntity  class_info,
const std::vector< CallableEntity > &  methods 
)
inline

Add a class type available in JavaScript code.

Note
No members definition here: In JavaScript this is done by the constructor

◆ eval()

void oe::ext::js::Runtime::eval ( const char *  data)
inline

Evaluate a script in the current runtime.

This method may be called multiple times (for example when appending multiples JS files)

◆ getJson()

static nlohmann::json oe::ext::js::Runtime::getJson ( duk_context *  ctx,
int  index 
)
inlinestatic

pull a nlhomann::json object from a parameter

Used to fetch json parameters from C++ functions

◆ pushJson()

static void oe::ext::js::Runtime::pushJson ( duk_context *  ctx,
const nlohmann::json &  data,
bool  is_object = true 
)
inlinestatic

push a nlhomann::json object as a json to the runtime

Parameters
ctxDuktape context
dataJSON data
is_objectdoes the object should be sent as an object (true) or as an array (false)
Note
object sent as array will be silently ignored by Duktape and replaced by an empty array

The documentation for this struct was generated from the following file: