Example #1
0
AJ_Status AJS_RegisterIO(duk_context* ctx, duk_idx_t ioIdx)
{
    duk_idx_t pinIdx;
    duk_idx_t i;
    uint16_t numPins = AJS_TargetIO_GetNumPins();

    /*
     * Create base pin protoype
     */
    pinIdx = duk_push_object(ctx);
    AJS_SetPropertyAccessors(ctx, pinIdx, "functions", NULL, NativeFunctionsGetter);
    AJS_SetPropertyAccessors(ctx, pinIdx, "info", NULL, NativeInfoGetter);
    /*
     * Create the pin objects
     */
    duk_push_array(ctx);
    for (i = 0; i < numPins; ++i) {
        AJS_CreateObjectFromPrototype(ctx, pinIdx);
        duk_push_int(ctx, i);
        duk_put_prop_string(ctx, -2, "id");
        duk_put_prop_index(ctx, -2, i);
    }
    duk_put_prop_string(ctx, ioIdx, "pin");
    duk_pop(ctx);
    /*
     * Register the native functions
     */
    AJS_PutFunctionList(ctx, ioIdx, io_native_functions, TRUE);
    /*
     * GPIO attribute constants
     */
    duk_put_number_list(ctx, ioIdx, io_constants);
    /*
     * A hidden property for keeping track of triggers
     */
    duk_push_string(ctx, AJS_HIDDEN_PROP("trigs"));
    duk_push_array(ctx);
    duk_def_prop(ctx, ioIdx, DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_HAVE_WRITABLE);

    /*
     * Compact the IO object and set it on the global object
     */
    duk_compact(ctx, ioIdx);
    duk_dup(ctx, ioIdx);
    duk_put_global_string(ctx, AJS_IOObjectName);
    return AJ_OK;
}
void AJS_RegisterMsgFunctions(AJ_BusAttachment* bus, duk_context* ctx, duk_idx_t ajIdx)
{
    duk_idx_t objIdx;

    duk_push_c_function(ctx, NativeBroadcastSignal, 2);
    duk_put_prop_string(ctx, ajIdx, "signal");

    duk_push_global_stash(ctx);
    /*
     * Push and initialized the peerProto object
     */
    objIdx = duk_push_object(ctx);
    duk_put_number_list(ctx, objIdx, peer_native_numbers);
    AJS_PutFunctionList(ctx, objIdx, peer_native_functions, TRUE);
    /*
     * Finalizer function called when the object is deleted
     */
    AJS_RegisterFinalizer(ctx, objIdx, NativeServiceObjectFinalizer);
    /*
     * Give the object a name
     */
    duk_put_prop_string(ctx, -2, "peerProto");
    duk_pop(ctx);
}