Exemplo n.º 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;
}
Exemplo n.º 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;
}
Exemplo n.º 3
0
int
to_erl_object(emonk_buf_t* buf, JSContext* cx, JSObject* obj)
{
    JSObject* iter;
    JSString* key;
    jsid idp;
    jsval val;
    jschar* keyname;
    size_t keylen;
    int count = 0;
    int lengthpos;
    
    REQUEST(7);

    BUFPTR[0] = SMALL_TUPLE;
    BUFPTR[1] = (char) 1;
    BUFPTR[2] = LIST;
    
    // Remember the byte offset where length goes so we can write it
    // after enumerating the properties.
    lengthpos = buf->used + 3;
    buf->used += 7;
    
    iter = JS_NewPropertyIterator(cx, obj);
    if(iter == NULL) return ERROR;
    
    while(JS_NextProperty(cx, iter, &idp))
    {
        // Done iterating, write length and bail.
        if(idp == JSVAL_VOID)
        {
            count = htonl(count);
            memcpy(buf->buf+lengthpos, &count, 4);

            REQUEST(1);
            BUFPTR[0] = NIL;
            buf->used += 1;
            
            return OK;
        }

        REQUEST(2);
        BUFPTR[0] = SMALL_TUPLE;
        BUFPTR[1] = 2;
        buf->used += 2;

        if(!JS_IdToValue(cx, idp, &val)) return ERROR;
        if(!to_erl_string(buf, cx, val)) return ERROR;
        
        key = JS_ValueToString(cx, val);
        keyname = JS_GetStringChars(key);
        keylen = JS_GetStringLength(key);
        
        if(!JS_GetUCProperty(cx, obj, keyname, keylen, &val)) return ERROR;
        if(!to_erl_intern(buf, cx, val)) return ERROR;
        count += 1;
    }

    return ERROR;
}
Exemplo n.º 4
0
int
to_erl_object(ErlNifEnv* env, JSContext* cx, JSObject* obj, ERL_NIF_TERM* term)
{
    ERL_NIF_TERM* array = NULL;
    ERL_NIF_TERM list;
    ERL_NIF_TERM keyterm;
    ERL_NIF_TERM valterm;
    JSObject* iter;
    jsid idp;
    jsval val;
    int length;
    int index;
    int ret = ERROR;

    iter = JS_NewPropertyIterator(cx, obj);
    if(iter == NULL) goto done;

    length = 0;
    while(JS_NextProperty(cx, iter, &idp))
    {
        if(idp == JSID_VOID) break;
        length += 1;
    }
    
    array = enif_alloc(length * sizeof(ERL_NIF_TERM));
    if(array == NULL) goto done;
    
    iter = JS_NewPropertyIterator(cx, obj);
    if(iter == NULL) goto done;

    index = 0;
    while(JS_NextProperty(cx, iter, &idp))
    {
        if(idp == JSID_VOID)
        {
            list = enif_make_list_from_array(env, array, length);
            *term = enif_make_tuple1(env, list);
            ret = OK;
            goto done;
        }

        if(!JS_IdToValue(cx, idp, &val)) goto done;
        if(!to_erl_string(env, cx, val, &keyterm)) goto done;
        if(!JS_GetPropertyById(cx, obj, idp, &val)) goto done;
        if(!to_erl_intern(env, cx, val, &valterm)) goto done;
        
        array[index] = enif_make_tuple2(env, keyterm, valterm);
        index += 1;
    }

done:
    if(array != NULL) enif_free(array);
    return ret;
}