lib/tcl-duktape.c
/*
 * Call a JS method/function.
 * Usage: call token method this ?{arg ?type?}? ...
 * Return value: the result of the method call coerced to string.
 * Side effects: may change the Duktape interpreter heap.
 */
Call a JS method/function.
Usage: call token method this ?{arg ?type?}.
Return value: the result of the method call coerced to string.
Side effects: may change the Duktape interpreter heap.
Call a JS method / function . Usage : call token method this ? { arg ?type? } . Return value : the result of the method call coerced to string . Side effects : may change the Duktape interpreter heap .
static int
CallMethod_Cmd(ClientData cdata, Tcl_Interp *interp, int objc,
        Tcl_Obj *const objv[])
{
    int i;
    int list_length;
    int tableIndex;
    int int_value;
    double double_value;
    duk_context *ctx;
    duk_int_t duk_result;
    Tcl_Obj *value;
    Tcl_Obj *type;
    static const char *types[] = {
        "boolean",
        "nan",
        "null",
        "number",
        "string",
        "undefined",
        (char *)NULL
    };
    enum types {
        TYPE_BOOLEAN,
        TYPE_NAN,
        TYPE_NULL,
        TYPE_NUMBER,
        TYPE_STRING,
        TYPE_UNDEFINED
    };
    if (objc < 4) {
        Tcl_WrongNumArgs(interp, 1, objv, USAGE_CALL_METHOD);
        return TCL_ERROR;
    }
    ctx = parse_id(cdata, interp, objv[1], 0);
    if (ctx == NULL) {
        return TCL_ERROR;
    }
    for (i = 2; i < 4; i++)
    {
        duk_result = duk_peval_string(ctx, Tcl_GetString(objv[i]));
        if (duk_result != 0) {
            Tcl_SetObjResult(interp,
                    Tcl_NewStringObj(
                        duk_safe_to_string(ctx, -1), -1));
            duk_pop(ctx);
            return TCL_ERROR;
        }
    }
    for (i = 4; i < objc; i++) {
        if (Tcl_ListObjIndex(interp, objv[i], 0, &value) != TCL_OK) {
            return TCL_ERROR;
        }
        if (Tcl_ListObjLength(interp, objv[i], &list_length) != TCL_OK) {
            return TCL_ERROR;
        }
        if (list_length == 2) {
            if (Tcl_ListObjIndex(interp, objv[i], 1, &type) != TCL_OK) {
                return TCL_ERROR;
            }
            if (Tcl_GetIndexFromObj(interp, type, types, "type", 0,
                    &tableIndex) != TCL_OK) {
                return TCL_ERROR;
            }
        } else if (list_length == 1) {
            tableIndex = TYPE_STRING;
        } else {
            Tcl_SetObjResult(interp, Tcl_NewStringObj(ERROR_ARG_LENGTH, -1));
            return TCL_ERROR;
        }
        switch ((enum types) tableIndex) {
            case TYPE_BOOLEAN:
                if (Tcl_GetIntFromObj(interp, value, &int_value) != TCL_OK) {
                    return TCL_ERROR;
                }
                duk_push_boolean(ctx, int_value);
                break;
            case TYPE_NAN:
                duk_push_nan(ctx);
                break;
            case TYPE_NULL:
                duk_push_null(ctx);
                break;
            case TYPE_NUMBER:
                if (Tcl_GetDoubleFromObj(interp, value,
                        &double_value) != TCL_OK) {
                    return TCL_ERROR;
                }
                duk_push_number(ctx, double_value);
                break;
            case TYPE_UNDEFINED:
                duk_push_undefined(ctx);
                break;
            case TYPE_STRING:
            default:
                duk_push_string(ctx, Tcl_GetString(value));
                break;
        }
    }
    duk_result = duk_pcall_method(ctx, objc - 4);
    Tcl_SetObjResult(interp, Tcl_NewStringObj(duk_safe_to_string(ctx, -1), -1));
    duk_pop(ctx);
    if (duk_result == 0) {
        return TCL_OK;
    } else {
        return TCL_ERROR;
    }
}
static int CallMethod_Cmd ( ClientData cdata , Tcl_Interp * interp , int objc , Tcl_Obj * const objv [ ] ) { int i ; int list_length ; int tableIndex ; int int_value ; double double_value ; duk_context * ctx ; duk_int_t duk_result ; Tcl_Obj * value ; Tcl_Obj * type ; static const char * types [ ] = { " " , " " , " " , " " , " " , " " , ( char * ) NULL } ; enum types { TYPE_BOOLEAN , TYPE_NAN , TYPE_NULL , TYPE_NUMBER , TYPE_STRING , TYPE_UNDEFINED } ; if ( objc < 4 ) { Tcl_WrongNumArgs ( interp , 1 , objv , USAGE_CALL_METHOD ) ; return TCL_ERROR ; } ctx = parse_id ( cdata , interp , objv [ 1 ] , 0 ) ; if ( ctx == NULL ) { return TCL_ERROR ; } for ( i = 2 ; i < 4 ; i ++ ) { duk_result = duk_peval_string ( ctx , Tcl_GetString ( objv [ i ] ) ) ; if ( duk_result != 0 ) { Tcl_SetObjResult ( interp , Tcl_NewStringObj ( duk_safe_to_string ( ctx , -1 ) , -1 ) ) ; duk_pop ( ctx ) ; return TCL_ERROR ; } } for ( i = 4 ; i < objc ; i ++ ) { if ( Tcl_ListObjIndex ( interp , objv [ i ] , 0 , & value ) != TCL_OK ) { return TCL_ERROR ; } if ( Tcl_ListObjLength ( interp , objv [ i ] , & list_length ) != TCL_OK ) { return TCL_ERROR ; } if ( list_length == 2 ) { if ( Tcl_ListObjIndex ( interp , objv [ i ] , 1 , & type ) != TCL_OK ) { return TCL_ERROR ; } if ( Tcl_GetIndexFromObj ( interp , type , types , " " , 0 , & tableIndex ) != TCL_OK ) { return TCL_ERROR ; } } else if ( list_length == 1 ) { tableIndex = TYPE_STRING ; } else { Tcl_SetObjResult ( interp , Tcl_NewStringObj ( ERROR_ARG_LENGTH , -1 ) ) ; return TCL_ERROR ; } switch ( ( enum types ) tableIndex ) { case TYPE_BOOLEAN : if ( Tcl_GetIntFromObj ( interp , value , & int_value ) != TCL_OK ) { return TCL_ERROR ; } duk_push_boolean ( ctx , int_value ) ; break ; case TYPE_NAN : duk_push_nan ( ctx ) ; break ; case TYPE_NULL : duk_push_null ( ctx ) ; break ; case TYPE_NUMBER : if ( Tcl_GetDoubleFromObj ( interp , value , & double_value ) != TCL_OK ) { return TCL_ERROR ; } duk_push_number ( ctx , double_value ) ; break ; case TYPE_UNDEFINED : duk_push_undefined ( ctx ) ; break ; case TYPE_STRING : default : duk_push_string ( ctx , Tcl_GetString ( value ) ) ; break ; } } duk_result = duk_pcall_method ( ctx , objc - 4 ) ; Tcl_SetObjResult ( interp , Tcl_NewStringObj ( duk_safe_to_string ( ctx , -1 ) , -1 ) ) ; duk_pop ( ctx ) ; if ( duk_result == 0 ) { return TCL_OK ; } else { return TCL_ERROR ; } }
Call a JS method/function.
Call a JS method / function .
/* Eval the function name and "this" to put them on the stack. */ /* Push the arguments. */