Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding dukglue_register_function_property #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

CallumCarmicheal
Copy link

@CallumCarmicheal CallumCarmicheal commented Aug 17, 2019

A helper function to allow the use of dukglue on Objects instead of the Global namespace.

Example code usage:

int math_add(int x, int y) {
    printf("Example function called\n");
    return x + y;
}

void CreateTheObject(duk_context *ctx) {
    // 1. Create a new object
    // 2. Register the function as a property on the object
    // 3. Store the object as global
    duk_push_object(ctx);
    dukglue_register_function_property(ctx, &math_add, "add");
    duk_put_global_string(ctx, "Math");
}

void CreateTheObjectWithIndex(duk_context *ctx) {
    // 1. Create a new object
    // 2. Register the function as a property on the object
    // 3. Store the object as global
    duk_idx_t mathObjectIdx = duk_push_object(ctx);

    // lets say for what ever reason there are more values on the stack
    for(int x = 0; x < 3; x++)
        duk_push_null(ctx);

    // We will now register the function to the original stack object's index (mathObjectIdx)
    dukglue_register_function_property(ctx, &math_add, mathObjectIdx, "add");

    // Clean up the nulls (only in the example)
    for(int x = 0; x < 3; x++)
        duk_pop(ctx);

    duk_put_global_string(ctx, "Math");
}

int PlayerOnHit(int currentHealth) {
    return currentHealth--;
}

void AppendToObject(duk_context *ctx) {
    // 1. Find the global object "Player"
    // 2. Register the function as a property
    // 3. Remove "Game" from the stack 
    duk_get_global_string(ctx, "Game");
    dukglue_register_function_property(ctx, &PlayerOnHit, "OnHit");
    duk_pop(ctx);
}

A helper function to allow the use of dukglue on Objects instead of the Global namespace.

Example code usage:

```cpp
int math_add(int x, int y) {
    printf("Example function called\n");
    return x + y;
}

void CreateTheObject(duk_context *ctx) {
    // 1. Create a new object
    // 2. Register the function as a property on the object
    // 3. Store the object as global
    duk_push_object(ctx);
    dukglue_register_function_property(ctx, &math_add, "add");
    duk_put_global_string(ctx, "Math");
}

void CreateTheObjectWithIndex(duk_context *ctx) {
    // 1. Create a new object
    // 2. Register the function as a property on the object
    // 3. Store the object as global
    duk_idx_t mathObjectIdx = duk_push_object(ctx);

    // lets say for what ever reason there are more values on the stack
    for(int x = 0; x < 3; x++)
        duk_push_null(ctx);

    // We will now register the function to the original stack object's index (mathObjectIdx)
    dukglue_register_function_property(ctx, &math_add, mathObjectIdx, "add");
    duk_put_global_string(ctx, "Math");

    // Clean up the nulls (only in the example)
    for(int x = 0; x < 3; x++)
        duk_pop(ctx);
}

int PlayerOnHit(int currentHealth) {
    return currentHealth--;
}

void AppendToObject(duk_context *ctx) {
    // 1. Find the global object "Player"
    // 2. Register the function as a property
    // 3. Remove "Game" from the stack 
    duk_get_global_string(ctx, "Game");
    dukglue_register_function_property(ctx, &PlayerOnHit, "OnHit");
    duk_pop(ctx);
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant