Exemplo n.º 1
0
native_netscape_javascript_JSObject_eval(
    JRIEnv* env,
    struct netscape_javascript_JSObject* self,
    struct java_lang_String *s)
{
#ifndef JAVA
    return NULL;
#else
    JSContext *cx;
    JSObject *jso;
    JSSavedState saved;
    const char *cstr;
    int cstrlen;
    struct java_lang_Object *ret;
    jsval rval;
    int cost = 0;
    JSPrincipals *principals;
    char *codebase;

    if (!enterJS(env, self, &cx, &jso, &saved))
        return NULL;

    if (! s ||
        ! (cstr = JRI_GetStringPlatformChars(env, s,
					     (const jbyte *) cx->charSetName,
					     (jint) cx->charSetNameLength))) {
        /* FIXME this should be an error of some sort */
        js_throwJSException(env, "illegal eval string");
        ret = NULL;
	goto do_exit;
    }

    /* subtract 1 for the NUL */
    /*
     * FIXME There is no JRI_GetStringPlatformLength (what a misnomer, if there
     * were!), but JRI_GetStringPlatformChars does NUL-terminate the encoded
     * string, so we trust that no charset encoding ever uses a NUL byte as
     * part of a multibyte sequence and just call strlen.
     */
    cstrlen = strlen(cstr);

    principals = js_GetJSPrincipalsFromJavaCaller(cx, 0);
    codebase = principals ? principals->codebase : NULL;
    if (! JS_EvaluateScriptForPrincipals(cx, jso, principals, cstr, cstrlen,
                                         codebase, 0, &rval) ||
        ! js_convertJSValueToJObject((HObject**)&ret, cx, rval,
                                     0, 0, JS_FALSE, &cost)) {
        ret = NULL;
    }

  do_exit:
    if (!exitJS(env, self, cx, jso, &saved))
        return NULL;

    return ret;
#endif
}
Exemplo n.º 2
0
PRBool nsJSSh::LoadURL(const char *url, jsval* retval)
{
  nsCOMPtr<nsIIOService> ioserv = do_GetService(NS_IOSERVICE_CONTRACTID);
  if (!ioserv) {
    NS_ERROR("could not get io service");
    return PR_FALSE;
  }

  nsCOMPtr<nsIChannel> channel;
  ioserv->NewChannel(nsDependentCString(url), nsnull, nsnull, getter_AddRefs(channel));
  if (!channel) {
    NS_ERROR("could not create channel");
    return PR_FALSE;
  }
  
  nsCOMPtr<nsIInputStream> instream;
  channel->Open(getter_AddRefs(instream));
  if (!instream) {
    NS_ERROR("could not open stream");
    return PR_FALSE;
  }

  nsCString buffer;
  nsAutoArrayPtr<char> buf(new char[1024]);
  if (!buf) {
    NS_ERROR("could not alloc buffer");
    return PR_FALSE;
  }

  PRUint32 bytesRead = 0;

  do {
    if (NS_FAILED(instream->Read(buf, 1024, &bytesRead))) {
      NS_ERROR("stream read error");
      return PR_FALSE;
    }
    buffer.Append(buf, bytesRead);
    LOG(("appended %d bytes:\n%s", bytesRead, buffer.get()));
  } while (bytesRead > 0);

  LOG(("loaded %d bytes:\n%s", buffer.Length(), buffer.get()));

  JS_BeginRequest(mJSContext);
  JSPrincipals *jsprincipals;
  mPrincipal->GetJSPrincipals(mJSContext, &jsprincipals);

  if(NS_FAILED(mContextStack->Push(mJSContext))) {
    NS_ERROR("failed to push the current JSContext on the nsThreadJSContextStack");
    return NS_ERROR_FAILURE;
  }

  jsval result;
  JSBool ok = JS_EvaluateScriptForPrincipals(mJSContext, mContextObj,
                                             jsprincipals, buffer.get(),
                                             buffer.Length(),
                                             url, 1, &result);
  JSPRINCIPALS_DROP(mJSContext, jsprincipals);

  JSContext *oldcx;
  mContextStack->Pop(&oldcx);
  NS_ASSERTION(oldcx == mJSContext, "JS thread context push/pop mismatch");

  if (ok && retval) *retval=result;
  
  JS_EndRequest(mJSContext);

  return ok;
}