Exemple #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;
}
/*
 * Called with and array of interfaces at the top of the stack. Replaces the interfaces array with
 * the service object on returning.
 */
static void AddServiceObject(duk_context* ctx, SessionInfo* sessionInfo, const char* path, const char* dest)
{
    /*
     * Create a service object from the prototype and customize it
     */
    AJS_GetGlobalStashObject(ctx, "peerProto");
    AJS_CreateObjectFromPrototype(ctx, -1);
    duk_remove(ctx, -2);
    /*
     * Set properties on the service object
     */
    duk_push_string(ctx, dest);
    duk_put_prop_string(ctx, -2, "dest");
    duk_push_string(ctx, path);
    duk_put_prop_string(ctx, -2, "path");
    duk_swap_top(ctx, -2);
    duk_put_prop_string(ctx, -2, "interfaces");
    /*
     * Increment the peer session refCount. It will be decremented when the service object
     * finalizer is called. If the refCount goes to zero and a session was established when
     * session will be closed.
     */
    ++sessionInfo->refCount;
}