コード例 #1
0
ファイル: engine_spidermonkey.cpp プロジェクト: xrogaan/mongo
    jsval toval( const char * c ) {
        JSString * s = JS_NewStringCopyZ( _context , c );
        if ( s )
            return STRING_TO_JSVAL( s );

        // possibly unicode, try manual

        size_t len = strlen( c );
        size_t dstlen = len * 4;
        jschar * dst = (jschar*)malloc( dstlen );

        JSBool res = JS_DecodeBytes( _context , c , len , dst, &dstlen );
        if ( res ) {
            s = JS_NewUCStringCopyN( _context , dst , dstlen );
        }

        free( dst );

        if ( ! res ) {
            cout << "decode failed. probably invalid utf-8 string [" << c << "]" << endl;
            jsval v;
            if ( JS_GetPendingException( _context , &v ) )
                cout << "\t why: " << toString( v ) << endl;
            throw UserException( 9006 , "invalid utf8" );
        }

        assert( s );
        return STRING_TO_JSVAL( s );
    }
コード例 #2
0
ファイル: testXDR.cpp プロジェクト: LyeSS/mozilla-central
static JSScript *
CompileScriptForPrincipalsVersionOrigin(JSContext *cx, JS::HandleObject obj,
                                        JSPrincipals *principals, JSPrincipals *originPrincipals,
                                        const char *bytes, size_t nbytes,
                                        const char *filename, unsigned lineno,
                                        JSVersion version)
{
    size_t nchars;
    if (!JS_DecodeBytes(cx, bytes, nbytes, NULL, &nchars))
        return NULL;
    jschar *chars = static_cast<jschar *>(JS_malloc(cx, nchars * sizeof(jschar)));
    if (!chars)
        return NULL;
    JS_ALWAYS_TRUE(JS_DecodeBytes(cx, bytes, nbytes, chars, &nchars));
    JSScript *script = JS_CompileUCScriptForPrincipalsVersionOrigin(cx, obj,
                                                                    principals, originPrincipals,
                                                                    chars, nchars,
                                                                    filename, lineno, version);
    free(chars);
    return script;
}
コード例 #3
0
ファイル: testXDR.cpp プロジェクト: JasonGross/mozjs
static JSScript *
CompileScriptForPrincipalsVersionOrigin(JSContext *cx, JS::HandleObject obj,
                                        JSPrincipals *originPrincipals,
                                        const char *bytes, size_t nbytes,
                                        const char *filename, unsigned lineno,
                                        JSVersion version)
{
    size_t nchars;
    if (!JS_DecodeBytes(cx, bytes, nbytes, nullptr, &nchars))
        return nullptr;
    jschar *chars = static_cast<jschar *>(JS_malloc(cx, nchars * sizeof(jschar)));
    if (!chars)
        return nullptr;
    JS_ALWAYS_TRUE(JS_DecodeBytes(cx, bytes, nbytes, chars, &nchars));
    JS::CompileOptions options(cx);
    options.setOriginPrincipals(originPrincipals)
           .setFileAndLine(filename, lineno)
           .setVersion(version);
    JSScript *script = JS::Compile(cx, obj, options, chars, nchars);
    free(chars);
    return script;
}
コード例 #4
0
ファイル: to_js.c プロジェクト: alepharchives/iErl
jsval
to_js_string(ErlNifEnv* env, JSContext* cx, ERL_NIF_TERM term)
{
    ErlNifBinary bin;
    JSString* str;
    jschar* chars;
    size_t charslen;
    
    if(!enif_inspect_binary(env, term, &bin))
    {
        return JSVAL_VOID;
    }
    
    if(!JS_DecodeBytes(cx, (char*) bin.data, bin.size, NULL, &charslen))
    {
        return JSVAL_VOID;
    }
        
    chars = JS_malloc(cx, (charslen + 1) * sizeof(jschar));
    if(chars == NULL) return JSVAL_VOID;
    
    if(!JS_DecodeBytes(cx, (char*) bin.data, bin.size, chars, &charslen))
    {
        JS_free(cx, chars);
        return JSVAL_VOID;
    }
    chars[charslen] = '\0';
    
    str = JS_NewUCString(cx, chars, charslen);
    if(!str)
    {
        JS_free(cx, chars);
        return JSVAL_VOID;
    }

    return STRING_TO_JSVAL(str);
}
コード例 #5
0
JSBool RemoteStagedService::evaluate(struct mg_connection *conn, void *closures)
{
	RemoteStagedService *that = (RemoteStagedService *) closures;
	mg_send_header(conn, "Content-Type", "application/json");
	if(!that->cleanUp())
	{
		HttpHandler::response(conn, NULL, RESP_FAIL_STR, ERR_CODE_GENERAL, "cleanUp");
		return JS_TRUE;
	}

	if(!(conn->query_string && (strlen(conn->query_string)<STG_RECEIVE_SRC_SIZE))) {
		HttpHandler::response(conn, NULL, RESP_FAIL_STR, ERR_CODE_INVALID_ARGS, "");
		return JS_TRUE;
	}

	JSString *queryStr = JS_InternString(that->cx, conn->query_string + 4);
	JSString *queryEscStr;
	if(!JS_UnescapedString(that->cx, queryStr, &queryEscStr))
	{
		HttpHandler::response(conn, NULL, RESP_FAIL_STR, ERR_CODE_GENERAL, "unescapeString");
		return JS_TRUE;
	}

	char *queryEscChars = JS_EncodeString(that->cx, queryEscStr);
	jsSrcBufferSize = STG_RECEIVE_SRC_SIZE;
	if(!JS_DecodeBytes(that->cx, queryEscChars, strlen(queryEscChars), jsSrcBuffer, &jsSrcBufferSize)) 
	{
		HttpHandler::response(conn, NULL, RESP_FAIL_STR, ERR_CODE_GENERAL, "JS_DecodeBytes");
		return JS_TRUE;
	}

	jsval parseVal;
	if (!reflect_parse_from_string(that->cx, jsSrcBuffer, jsSrcBufferSize, &parseVal))
	{
		HttpHandler::response(conn, NULL, RESP_FAIL_STR, ERR_CODE_REFLECT_PARSE, "reflection parse error");
		return JS_TRUE;
	}

	JS::Value unparseArgs[] = { parseVal  };
	JS::Value unparseRes;
	if (!JS_CallFunctionName(that->cx, that->global, "unparse", 1, unparseArgs, &unparseRes)){
		HttpHandler::response(conn, NULL, RESP_FAIL_STR, ERR_CODE_GENERAL, "unparse");
		return JS_TRUE;
	}

	JS::Value *stringifyArg = { &OBJECT_TO_JSVAL(that->respArray) };
	if(!JS_Stringify(that->cx, stringifyArg, 0, js::NullValue(), onJSONStringifyFinished, 0)) 
	{
		HttpHandler::response(conn, NULL, RESP_FAIL_STR, ERR_CODE_GENERAL, "JS_Stringify");
		return JS_TRUE;
	}

	char srcBuffer[STG_RECEIVE_SRC_SIZE];
	size_t srcBufferSize=STG_RECEIVE_SRC_SIZE;
  if (!DeflateStringToBuffer(that->cx, jsSrcBuffer, jsSrcBufferSize, srcBuffer, &srcBufferSize)) 
	{
		HttpHandler::response(conn, NULL, RESP_FAIL_STR, ERR_CODE_GENERAL, "DeflateStringToBuffer");
		return JS_TRUE;
  }
	srcBuffer[srcBufferSize] = 0;

	HttpHandler::response(conn, NULL, RESP_SUCCESS_STR, srcBuffer);
	return JS_TRUE;
}