Esempio n. 1
0
JSBool JavaObject::getProperty(JSContext* ctx, JSObject* obj, jsid id,
    jsval* rval) {
  Debug::log(Debug::Spam) << "JavaObject::getProperty obj=" << obj << Debug::flush;
  SessionData* data = JavaObject::getSessionData(ctx, obj);
  if (!data) {
    // TODO: replace the frame with an error page instead?
    *rval = JSVAL_VOID;
    return JS_TRUE;
  }
  int objectRef = JavaObject::getObjectId(ctx, obj);
  if (JSID_IS_STRING(id)) {
    JSString* str = JSID_TO_STRING(id);
    if ((JS_GetStringEncodingLength(ctx, str) == 8) && !strncmp("toString",
          JS_EncodeString(ctx, str), 8)) {
      *rval = data->getToStringTearOff();
      return JS_TRUE;
    }
    if ((JS_GetStringEncodingLength(ctx, str) == 2) && !strncmp("id",
          JS_EncodeString(ctx, str), 2)) {
      *rval = INT_TO_JSVAL(objectRef);
      return JS_TRUE;
    }
    if ((JS_GetStringEncodingLength(ctx, str) == 16) && !strncmp("__noSuchMethod__",
          JS_EncodeString(ctx, str), 16)) {
      // Avoid error spew if we are disconnected
      *rval = JSVAL_VOID;
      return JS_TRUE;
    }
    // TODO: dumpJsVal can no longer handle this case
    //Debug::log(Debug::Error) << "Getting unexpected string property "
    //    << dumpJsVal(ctx, id) << Debug::flush;
    // TODO: throw a better exception here
    return JS_FALSE;
  }
  if (!JSID_IS_INT(id)) {
    // TODO: dumpJsVal can no longer handle this case
    //Debug::log(Debug::Error) << "Getting non-int/non-string property "
    //      << dumpJsVal(ctx, id) << Debug::flush;
    // TODO: throw a better exception here
    return JS_FALSE;
  }
  int dispId = JSID_TO_INT(id);

  HostChannel* channel = data->getHostChannel();
  SessionHandler* handler = data->getSessionHandler();

  gwt::Value value = ServerMethods::getProperty(*channel, handler, objectRef, dispId);
  data->makeJsvalFromValue(*rval, ctx, value);
  return JS_TRUE;
}
Esempio n. 2
0
rpmRC rpmjsRun(rpmjs js, const char * str, const char ** resultp)
{
    rpmRC rc = RPMRC_FAIL;

    if (!(str && *str))
	goto exit;

    if (js == NULL) js = rpmjsI();

#if defined(WITH_GPSEE)
    {	gpsee_interpreter_t * I = js->I;
	jsval v = JSVAL_VOID;
	JSBool ok = JS_EvaluateScript(I->cx, I->realm->globalObject,
				str, strlen(str), __FILE__, __LINE__, &v);
	if (!ok)
	    goto exit;
	rc = RPMRC_OK;
	if (resultp && JSVAL_IS_STRING(v)) {
	    JSString * rstr = JSVAL_TO_STRING(v);
	    size_t ns = JS_GetStringEncodingLength(I->cx, rstr);
	    char * s = xmalloc(ns+1);
	    ns = JS_EncodeStringToBuffer(rstr, s, ns);
	    s[ns] = '\0';
	    *resultp = s;
	}
    }
#endif	/* WITH_GPSEE */

exit:
RPMJSDBG(0, (stderr, "<== %s(%p,%p[%u]) rc %d\n", __FUNCTION__, js, str, (unsigned)(str ? strlen(str) : 0), rc));

    return rc;
}
Esempio n. 3
0
nsresult
XPCJSStackFrame::CreateStack(JSContext* cx, JSStackFrame* fp,
                             XPCJSStackFrame** stack)
{
    static const unsigned MAX_FRAMES = 100;
    unsigned numFrames = 0;

    nsRefPtr<XPCJSStackFrame> first = new XPCJSStackFrame();
    nsRefPtr<XPCJSStackFrame> self = first;
    while (fp && self) {
        if (!JS_IsScriptFrame(cx, fp)) {
            self->mLanguage = nsIProgrammingLanguage::CPLUSPLUS;
        } else {
            self->mLanguage = nsIProgrammingLanguage::JAVASCRIPT;
            JSScript* script = JS_GetFrameScript(cx, fp);
            jsbytecode* pc = JS_GetFramePC(cx, fp);
            if (script && pc) {
                JS::AutoEnterFrameCompartment ac;
                if (ac.enter(cx, fp)) {
                    const char* filename = JS_GetScriptFilename(cx, script);
                    if (filename) {
                        self->mFilename = (char*)
                            nsMemory::Clone(filename,
                                            sizeof(char)*(strlen(filename)+1));
                    }

                    self->mLineno = (int32_t) JS_PCToLineNumber(cx, script, pc);

                    JSFunction* fun = JS_GetFrameFunction(cx, fp);
                    if (fun) {
                        JSString *funid = JS_GetFunctionId(fun);
                        if (funid) {
                            size_t length = JS_GetStringEncodingLength(cx, funid);
                            if (length != size_t(-1)) {
                                self->mFunname = static_cast<char *>(nsMemory::Alloc(length + 1));
                                if (self->mFunname) {
                                    JS_EncodeStringToBuffer(funid, self->mFunname, length);
                                    self->mFunname[length] = '\0';
                                }
                            }
                        }
                    }
                }
            } else {
                self->mLanguage = nsIProgrammingLanguage::CPLUSPLUS;
            }
        }

        if (++numFrames > MAX_FRAMES) {
            fp = NULL;
        } else if (JS_FrameIterator(cx, &fp)) {
            XPCJSStackFrame* frame = new XPCJSStackFrame();
            self->mCaller = frame;
            self = frame;
        }
    }

    *stack = first.forget().get();
    return NS_OK;
}
Esempio n. 4
0
gboolean
gjs_string_to_utf8 (JSContext  *context,
                    const jsval value,
                    char      **utf8_string_p)
{
    JSString *str;
    gsize len;
    char *bytes;

    JS_BeginRequest(context);

    if (!JSVAL_IS_STRING(value)) {
        gjs_throw(context,
                  "Value is not a string, cannot convert to UTF-8");
        JS_EndRequest(context);
        return JS_FALSE;
    }

    str = JSVAL_TO_STRING(value);

    len = JS_GetStringEncodingLength(context, str);
    if (len == (gsize)(-1)) {
        JS_EndRequest(context);
        return JS_FALSE;
    }

    if (utf8_string_p) {
        bytes = JS_EncodeStringToUTF8(context, str);
        *utf8_string_p = bytes;
    }

    JS_EndRequest(context);

    return JS_TRUE;
}
Esempio n. 5
0
bool
gjs_string_to_utf8 (JSContext      *context,
                    const JS::Value value,
                    char          **utf8_string_p)
{
    gsize len;
    char *bytes;

    JS_BeginRequest(context);

    if (!value.isString()) {
        gjs_throw(context,
                  "Value is not a string, cannot convert to UTF-8");
        JS_EndRequest(context);
        return false;
    }

    JS::RootedString str(context, value.toString());

    len = JS_GetStringEncodingLength(context, str);
    if (len == (gsize)(-1)) {
        JS_EndRequest(context);
        return false;
    }

    if (utf8_string_p) {
        bytes = JS_EncodeStringToUTF8(context, str);
        *utf8_string_p = bytes;
    }

    JS_EndRequest(context);

    return true;
}
Esempio n. 6
0
nsresult
XPCJSStackFrame::CreateStack(JSContext* cx, XPCJSStackFrame** stack)
{
    static const unsigned MAX_FRAMES = 100;

    nsRefPtr<XPCJSStackFrame> first = new XPCJSStackFrame();
    nsRefPtr<XPCJSStackFrame> self = first;

    JS::StackDescription* desc = JS::DescribeStack(cx, MAX_FRAMES);
    if (!desc)
        return NS_ERROR_FAILURE;

    for (size_t i = 0; i < desc->nframes && self; i++) {
        self->mLanguage = nsIProgrammingLanguage::JAVASCRIPT;

	JSAutoCompartment ac(cx, desc->frames[i].script);
        const char* filename = JS_GetScriptFilename(cx, desc->frames[i].script);
        if (filename) {
            self->mFilename = (char*)
                nsMemory::Clone(filename,
                                sizeof(char)*(strlen(filename)+1));
        }

        self->mLineno = desc->frames[i].lineno;

        JSFunction* fun = desc->frames[i].fun;
        if (fun) {
            JS::RootedString funid(cx, JS_GetFunctionDisplayId(fun));
            if (funid) {
                size_t length = JS_GetStringEncodingLength(cx, funid);
                if (length != size_t(-1)) {
                    self->mFunname = static_cast<char *>(nsMemory::Alloc(length + 1));
                    if (self->mFunname) {
                        JS_EncodeStringToBuffer(cx, funid, self->mFunname, length);
                        self->mFunname[length] = '\0';
                    }
                }
            }
        }

        XPCJSStackFrame* frame = new XPCJSStackFrame();
        self->mCaller = frame;
        self = frame;
    }

    JS::FreeStackDescription(cx, desc);

    *stack = first.forget().get();
    return NS_OK;
}
Esempio n. 7
0
static JSBool _egueb_script_js_sm_scripter_alert(JSContext *cx, uintN argc, jsval *vp)
{
	JSString* u16_txt;
	unsigned int length;
	char *txt;

	if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, vp), "S", &u16_txt))
		return JS_FALSE;

	length = JS_GetStringEncodingLength(cx, u16_txt);
	txt = alloca(sizeof(char) * (length + 1));
	JS_EncodeStringToBuffer(u16_txt, txt, length);

	printf("%.*s\n", length, txt);
	return JS_TRUE;
}
Esempio n. 8
0
int run(JSContext * cx)
{
	/* Enter a request before running anything in the context */
	JSAutoRequest ar(cx);

	/* Create the global object in a new compartment. */
	JSObject *global = JS_NewGlobalObject(cx, &global_class, nullptr);
	if (!global)
		return 3;

	/* Set the context's global */
	JSAutoCompartment ac(cx, global);
	JS_SetGlobalObject(cx, global);

	/* Populate the global object with the standard globals, like Object and Array. */
	if (!JS_InitStandardClasses(cx, global))
		 return 4;

	/* Your application code here. This may include JSAPI calls to create your own custom JS objects and run scripts. */
	js::RootedValue rval(cx);
	int rc = JS_EvaluateScript(cx, global, runCommand, strlen(runCommand),
				   "TestCommand", 0, rval.address());
	if (!rc)
		return 5;

	if (!JSVAL_IS_STRING(rval))
		return 0;

/* Result of the command is a string, print it out. */
	js::RootedString str(cx, JSVAL_TO_STRING(rval));
	size_t encodedLength = JS_GetStringEncodingLength(cx, str);
	rc = 0;
	char *buffer = (char *)malloc(encodedLength + 1);
	buffer[encodedLength] = '\0';
	size_t result = JS_EncodeStringToBuffer(cx, str, buffer, encodedLength);
	if (result == (size_t) - 1)
		rc = 6;
	else
		puts(buffer);
	free(buffer);

	return rc;
}
Esempio n. 9
0
bool GetOVRString(JSContext* cx, JS::HandleString s, OVR::String* out) {
  // Fill a buffer with the string data
  size_t sLen = JS_GetStringEncodingLength(cx, s) * sizeof(char);
  char* sBuf = new char[sLen+1];
  size_t sCopiedLen = JS_EncodeStringToBuffer(cx, s, sBuf, sLen);
  if (sCopiedLen != sLen) {
    delete[] sBuf;
    JS_ReportError(cx, "Could not encode output");
    return false;
  }
  sBuf[sLen] = '\0';

  // Create the string to return from the buffer
  // TODO: See whether we can avoid this extra string copy
  *out = OVR::String(sBuf);

  // Make sure we free the buffer before returning our string
  delete[] sBuf;
  
  return true;
}