lib/tcl-duktape.c
/*
 * Convert JavaScript item to a Tcl item
 */
Convert JavaScript item to a Tcl item
Convert JavaScript item to a Tcl item
static Tcl_Obj *Tclduk_JSToTcl(duk_context *ctx, duk_idx_t idx) {
    const char *dukString;
    duk_size_t dukStringLength;
    duk_int_t arrayLength;
    duk_idx_t arrayIndex;
    Tcl_Obj *dukStringObj, *dukItemObj;
    enum {
        TCLDUK_TYPE_BYTEARRAY,
        TCLDUK_TYPE_STRING
    } string_format;
    dukString = NULL;
    dukStringObj = NULL;
    string_format = TCLDUK_TYPE_STRING;
    if (duk_check_type_mask(ctx, idx, DUK_TYPE_MASK_NULL | DUK_TYPE_MASK_UNDEFINED)) {
        dukString = "";
        dukStringLength = 0;
    }
    if (!dukString && !dukStringObj && duk_is_array(ctx, idx)) {
        duk_get_prop_string(ctx, idx, "length");
        arrayLength = duk_get_int(ctx, -1);
        duk_pop(ctx);
        dukStringObj = Tcl_NewObj();
        for (arrayIndex = 0; arrayIndex < arrayLength; arrayIndex++) {
            duk_get_prop_index(ctx, idx, arrayIndex);
            dukItemObj = Tclduk_JSToTcl(ctx, -1);
            duk_pop(ctx);
            Tcl_ListObjAppendElement(NULL, dukStringObj, dukItemObj);
        }
    }
    if (!dukString && !dukStringObj && duk_is_buffer_data(ctx, idx)) {
        duk_buffer_to_string(ctx, idx);
        string_format = TCLDUK_TYPE_BYTEARRAY;
    }
    if (!dukString && !dukStringObj && duk_check_type(ctx, idx, DUK_TYPE_OBJECT)) {
        duk_json_encode(ctx, idx);
    }
    if (!dukStringObj) {
        if (!dukString) {
            dukString = duk_safe_to_lstring(ctx, idx, &dukStringLength);
        }
        if (!dukString) {
            return(NULL);
        }
        switch (string_format) {
            case TCLDUK_TYPE_BYTEARRAY:
                dukStringObj = Tcl_NewByteArrayObj((const unsigned char *) dukString, dukStringLength);
                break;
            case TCLDUK_TYPE_STRING:
                dukStringObj = Tcl_NewStringObj(dukString, dukStringLength);
                break;
        }
    }
    return(dukStringObj);
}
static Tcl_Obj * Tclduk_JSToTcl ( duk_context * ctx , duk_idx_t idx ) { const char * dukString ; duk_size_t dukStringLength ; duk_int_t arrayLength ; duk_idx_t arrayIndex ; Tcl_Obj * dukStringObj , * dukItemObj ; enum { TCLDUK_TYPE_BYTEARRAY , TCLDUK_TYPE_STRING } string_format ; dukString = NULL ; dukStringObj = NULL ; string_format = TCLDUK_TYPE_STRING ; if ( duk_check_type_mask ( ctx , idx , DUK_TYPE_MASK_NULL | DUK_TYPE_MASK_UNDEFINED ) ) { dukString = " " ; dukStringLength = 0 ; } if ( ! dukString && ! dukStringObj && duk_is_array ( ctx , idx ) ) { duk_get_prop_string ( ctx , idx , " " ) ; arrayLength = duk_get_int ( ctx , -1 ) ; duk_pop ( ctx ) ; dukStringObj = Tcl_NewObj ( ) ; for ( arrayIndex = 0 ; arrayIndex < arrayLength ; arrayIndex ++ ) { duk_get_prop_index ( ctx , idx , arrayIndex ) ; dukItemObj = Tclduk_JSToTcl ( ctx , -1 ) ; duk_pop ( ctx ) ; Tcl_ListObjAppendElement ( NULL , dukStringObj , dukItemObj ) ; } } if ( ! dukString && ! dukStringObj && duk_is_buffer_data ( ctx , idx ) ) { duk_buffer_to_string ( ctx , idx ) ; string_format = TCLDUK_TYPE_BYTEARRAY ; } if ( ! dukString && ! dukStringObj && duk_check_type ( ctx , idx , DUK_TYPE_OBJECT ) ) { duk_json_encode ( ctx , idx ) ; } if ( ! dukStringObj ) { if ( ! dukString ) { dukString = duk_safe_to_lstring ( ctx , idx , & dukStringLength ) ; } if ( ! dukString ) { return ( NULL ) ; } switch ( string_format ) { case TCLDUK_TYPE_BYTEARRAY : dukStringObj = Tcl_NewByteArrayObj ( ( const unsigned char * ) dukString , dukStringLength ) ; break ; case TCLDUK_TYPE_STRING : dukStringObj = Tcl_NewStringObj ( dukString , dukStringLength ) ; break ; } } return ( dukStringObj ) ; }
Convert JavaScript item to a Tcl item
Convert JavaScript item to a Tcl item
/*
     * Convert types
     *     buffer          => ByteArray
     *     array           => List
     *     null/undefined  => empty string
     *     other object    => JSON
     *     everything else => string
     */