Пример #1
0
int
to_erl_intern(ErlNifEnv* env, JSContext* cx, jsval val, ERL_NIF_TERM* term)
{
    JSObject* obj = NULL;
    JSType type = JS_TypeOfValue(cx, val);
        
    if(val == JSVAL_NULL)
    {
        return to_erl_atom(env, "null", term);
    }
    else if(val == JSVAL_VOID)
    {
        // return ERROR;
        return to_erl_atom(env, "undefined", term);
    }
    else if(type == JSTYPE_BOOLEAN)
    {
        if(val == JSVAL_TRUE)
            return to_erl_atom(env, "true", term);
        else
            return to_erl_atom(env, "false", term);
    }
    else if(type == JSTYPE_STRING)
    {
        return to_erl_string(env, cx, val, term);
    }
    else if(type == JSTYPE_XML)
    {
        return to_erl_string(env, cx, val, term);
    }
    else if(type == JSTYPE_NUMBER)
    {
        if(JSVAL_IS_INT(val))
            return to_erl_int(env, cx, val, term);
        else
            return to_erl_float(env, cx, val, term);
    }
    else if(type == JSTYPE_OBJECT)
    {
        obj = JSVAL_TO_OBJECT(val);

        if(OK == to_erl_convert(env, cx, obj, term))
        {
            return OK;
        }
        
        if(JS_IsArrayObject(cx, obj))
        {
            return to_erl_array(env, cx, obj, term);
        }

        return to_erl_object(env, cx, obj, term);
    }

    return ERROR;
}
Пример #2
0
int
to_erl_intern(emonk_buf_t* buf, JSContext* cx, jsval val)
{
    JSObject* obj = NULL;
    JSType type = JS_TypeOfValue(cx, val);
    int status = ERROR;
        
    if(val == JSVAL_NULL)
    {
        return to_erl_atom(buf, "null", 4);
    }
    else if(val == JSVAL_VOID)
    {
        JS_ReportError(cx, "Cannot encode 'undefined' value as JSON");
        return ERROR;
    }
    else if(type == JSTYPE_BOOLEAN)
    {
        if(val == JSVAL_TRUE)
            return to_erl_atom(buf, "true", 4);
        else
            return to_erl_atom(buf, "false", 5);
    }
    else if(type == JSTYPE_STRING)
    {
        return to_erl_string(buf, cx, val);
    }
    else if(type == JSTYPE_XML)
    {
        return to_erl_string(buf, cx, val);
    }
    else if(type == JSTYPE_NUMBER)
    {
        if(JSVAL_IS_INT(val))
            return to_erl_int(buf, cx, val);
        else
            return to_erl_float(buf, cx, val);
    }
    else if(type == JSTYPE_OBJECT)
    {
        obj = JSVAL_TO_OBJECT(val);
        status = to_erl_from_handler(buf, cx, obj);
        if(status != IGNORE) return status;
        
        if(JS_IsArrayObject(cx, obj))
        {
            return to_erl_array(buf, cx, obj);
        }
        return to_erl_object(buf, cx, obj);
    }
    
    return ERROR;
}