Beispiel #1
0
js_compare_atom_keys(const void *k1, const void *k2)
{
    jsval v1, v2;

    v1 = (jsval)k1, v2 = (jsval)k2;
    if (JSVAL_IS_STRING(v1) && JSVAL_IS_STRING(v2))
	return !js_CompareStrings(JSVAL_TO_STRING(v1), JSVAL_TO_STRING(v2));
    if (JSVAL_IS_DOUBLE(v1) && JSVAL_IS_DOUBLE(v2))
	return *JSVAL_TO_DOUBLE(v1) == *JSVAL_TO_DOUBLE(v2);
    return v1 == v2;
}
Beispiel #2
0
/*
 * This function is used to convert function arguments and return value (jsval)
 * into the following based on each value's type tag:
 *
 *      jsval      returned
 *      -------------------
 *      STRING  -> char *
 *      INT     -> int
 *      DOUBLE  -> double *
 *      BOOLEAN -> int
 *      OBJECT  -> void *
 *
 * All are presented as void * for DTrace consumers to use, after shifting or
 * masking out the JavaScript type bits. This allows D scripts to use ints and
 * booleans directly and copyinstr() for string arguments, when types are known
 * beforehand.
 *
 * This is used by the function-args and function-rval probes, which also
 * provide raw (unmasked) jsvals should type info be useful from D scripts.
 */
void *
jsdtrace_jsvaltovoid(JSContext *cx, jsval argval)
{
    JSType type = TYPEOF(cx, argval);

    switch (type) {
      case JSTYPE_NULL:
      case JSTYPE_VOID:
        return JS_TYPE_STR(type);

      case JSTYPE_BOOLEAN:
        return (void *)JSVAL_TO_BOOLEAN(argval);

      case JSTYPE_STRING:
        return (void *)js_GetStringBytes(cx, JSVAL_TO_STRING(argval));

      case JSTYPE_NUMBER:
        if (JSVAL_IS_INT(argval))
            return (void *)JSVAL_TO_INT(argval);
        return JSVAL_TO_DOUBLE(argval);

      default:
        return JSVAL_TO_GCTHING(argval);
    }
    /* NOTREACHED */
}
Beispiel #3
0
char* DLLCALL
js_sprintf(JSContext *cx, uint argn, uintN argc, jsval *argv)
{
	char*		p;
    JSString*	str;

	if((p=js_ValueToStringBytes(cx, argv[argn++], NULL))==NULL)
		return(NULL);

	p=xp_asprintf_start(p);
    for(; argn<argc; argn++) {
		if(JSVAL_IS_DOUBLE(argv[argn]))
			p=xp_asprintf_next(p,XP_PRINTF_CONVERT|XP_PRINTF_TYPE_DOUBLE,*JSVAL_TO_DOUBLE(argv[argn]));
		else if(JSVAL_IS_INT(argv[argn]))
			p=xp_asprintf_next(p,XP_PRINTF_CONVERT|XP_PRINTF_TYPE_INT,JSVAL_TO_INT(argv[argn]));
		else if(JSVAL_IS_BOOLEAN(argv[argn]) && xp_printf_get_type(p)!=XP_PRINTF_TYPE_CHARP)
			p=xp_asprintf_next(p,XP_PRINTF_CONVERT|XP_PRINTF_TYPE_INT,JSVAL_TO_BOOLEAN(argv[argn]));
		else {
			if((str=JS_ValueToString(cx, argv[argn]))==NULL)
			    return(NULL);
			p=xp_asprintf_next(p,XP_PRINTF_CONVERT|XP_PRINTF_TYPE_CHARP,JS_GetStringBytes(str));
		}
	}

	return xp_asprintf_end(p, NULL);

}
Beispiel #4
0
JSBool
js_ValueToObject(JSContext *cx, jsval v, JSObject **objp)
{
    JSObject *obj;

    if (JSVAL_IS_NULL(v) || JSVAL_IS_VOID(v)) {
	obj = NULL;
    } else if (JSVAL_IS_OBJECT(v)) {
	obj = JSVAL_TO_OBJECT(v);
	if (!obj->map->clasp->convert(cx, obj, JSTYPE_OBJECT, &v))
	    return JS_FALSE;
	if (JSVAL_IS_OBJECT(v))
	    obj = JSVAL_TO_OBJECT(v);
    } else {
	if (JSVAL_IS_STRING(v)) {
	    obj = js_StringToObject(cx, JSVAL_TO_STRING(v));
	} else if (JSVAL_IS_INT(v)) {
	    obj = js_NumberToObject(cx, (jsdouble)JSVAL_TO_INT(v));
	} else if (JSVAL_IS_DOUBLE(v)) {
	    obj = js_NumberToObject(cx, *JSVAL_TO_DOUBLE(v));
	} else {
	    PR_ASSERT(JSVAL_IS_BOOLEAN(v));
	    obj = js_BooleanToObject(cx, JSVAL_TO_BOOLEAN(v));
	}
	if (!obj)
	    return JS_FALSE;
    }
    *objp = obj;
    return JS_TRUE;
}
Beispiel #5
0
static PyObject *to_python_object(JSContext *context, jsval value) {
    if (JSVAL_IS_PRIMITIVE(value)) {
        if (JSVAL_IS_STRING(value)) {
            return PyUnicode_FromString(JS_EncodeString(context, JSVAL_TO_STRING(value)));
        } else if (JSVAL_IS_BOOLEAN(value)) {
            return PyBool_FromLong(JSVAL_TO_BOOLEAN(value));
        } else if (JSVAL_IS_INT(value)) {
            return PyLong_FromLong(JSVAL_TO_INT(value));
        } else if (JSVAL_IS_DOUBLE(value)) {
            return PyFloat_FromDouble(JSVAL_TO_DOUBLE(value));
        } else {
            Py_INCREF(Py_None);
            return Py_None;
        }
    } else {
        JSObject *obj = JSVAL_TO_OBJECT(value);
        if (JS_ObjectIsDate(context, obj)) {
            return to_python_datetime(context, obj);
        } else if (JS_IsArrayObject(context, obj)) {
            return to_python_list(context, obj);
        } else {
            return to_python_dict(context, obj);
        }
    }
}
Beispiel #6
0
jsdouble
jsd_GetValueDouble(JSDContext* jsdc, JSDValue* jsdval)
{
    if(!JSVAL_IS_DOUBLE(jsdval->val))
        return 0;
    return JSVAL_TO_DOUBLE(jsdval->val);
}
Beispiel #7
0
JSBool WvideoVol(JSContext *cx, uintN argc, jsval *vp){
	JSObject *ob = NULL;
	ob = JSVAL_TO_OBJECT(JS_THIS(cx,vp));
	if(ob){
		video *vid = (video*)JS_GetPrivate(cx,ob);
		if(!vid) return JS_TRUE;
		if(argc = 0 || (!JSVAL_IS_DOUBLE(JS_ARGV(cx,vp)[0]) && !JSVAL_IS_INT(JS_ARGV(cx,vp)[0]))){
			double vol;
			vol = vid->vb->getVideoVol(vid);
			//print("Got value %f\n",vol);
			JS_SET_RVAL(cx,vp,INT_TO_JSVAL((int) (vol*100)));
		} else {
			double vol;
			if(JSVAL_IS_DOUBLE(JS_ARGV(cx,vp)[0]))
				vol = JSVAL_TO_DOUBLE(JS_ARGV(cx,vp)[0])/100;
			else if (JSVAL_IS_INT(JS_ARGV(cx,vp)[0]))
				vol = ((double)JSVAL_TO_INT(JS_ARGV(cx,vp)[0]))/100;
			if(vol < 0) vol = 0;
			if(vol > 1) vol = 1;
			vid->vb->setVideoVol(vid,vol);
			JS_SET_RVAL(cx,vp,INT_TO_JSVAL((int)vol * 100));
		}
	}
	return JS_TRUE;
}
Beispiel #8
0
double Context::FromJSArg<double>(const JS::Value &val) const
{
  if(!JSVAL_IS_NUMBER(val)) {
    return 0.0f;
  }
  return JSVAL_TO_DOUBLE(val);
}
Beispiel #9
0
JSBool
js_ValueToBoolean(JSContext *cx, jsval v, JSBool *bp)
{
    JSBool b;
    jsdouble d;

#if defined XP_PC && defined _MSC_VER && _MSC_VER <= 800
    /* MSVC1.5 coredumps */
    if (!bp)
	return JS_TRUE;
    /* This should be an if-else chain, but MSVC1.5 crashes if it is. */
#define ELSE
#else
#define ELSE else
#endif

    if (JSVAL_IS_NULL(v) || JSVAL_IS_VOID(v)) {
	/* Must return early to avoid falling thru to JSVAL_IS_OBJECT case. */
	*bp = JS_FALSE;
	return JS_TRUE;
    }
    if (JSVAL_IS_OBJECT(v)) {
	if (!JSVERSION_IS_ECMA(cx->version)) {
	    if (!OBJ_DEFAULT_VALUE(cx, JSVAL_TO_OBJECT(v), JSTYPE_BOOLEAN, &v))
		return JS_FALSE;
	    if (!JSVAL_IS_BOOLEAN(v))
		v = JSVAL_TRUE;		/* non-null object is true */
	    b = JSVAL_TO_BOOLEAN(v);
	} else {
	    b = JS_TRUE;
	}
    } ELSE
    if (JSVAL_IS_STRING(v)) {
	b = JSSTRING_LENGTH(JSVAL_TO_STRING(v)) ? JS_TRUE : JS_FALSE;
    } ELSE
    if (JSVAL_IS_INT(v)) {
	b = JSVAL_TO_INT(v) ? JS_TRUE : JS_FALSE;
    } ELSE
    if (JSVAL_IS_DOUBLE(v)) {
	d = *JSVAL_TO_DOUBLE(v);
	b = (!JSDOUBLE_IS_NaN(d) && d != 0) ? JS_TRUE : JS_FALSE;
    } ELSE
#if defined XP_PC && defined _MSC_VER && _MSC_VER <= 800
    if (JSVAL_IS_BOOLEAN(v)) {
	b = JSVAL_TO_BOOLEAN(v);
    }
#else
    {
        JS_ASSERT(JSVAL_IS_BOOLEAN(v));
	b = JSVAL_TO_BOOLEAN(v);
    }
#endif

#undef ELSE
    *bp = b;
    return JS_TRUE;
}
double TraceMonkeyValue::getFloat()
{
    assert(isValid());

    Logging::log(Logging::DEBUG, "TraceMonkeyValue::getFloat: \r\n"); printJSVAL(value);

    assert(JSVAL_IS_DOUBLE(value));
    return *(JSVAL_TO_DOUBLE(value));
}
Beispiel #11
0
SEXP
jsval_to_double(SEXP ptr)
{
    jsval *p = R_ExternalPtrAddr(ptr);
    if(!JSVAL_IS_DOUBLE(*p)) {
	PROBLEM "jsval is not a double!"
	    ERROR;
    }
    return(ScalarReal( * JSVAL_TO_DOUBLE(*p)));
}
float CFBFunctionsJS::getSpeed(const CFBCard& co)
{
    auto res = callJSFunc("GetSpeed", "j", createJsvalFromCard(co));
    if (JSVAL_IS_DOUBLE(res))
    {
        return (float)JSVAL_TO_DOUBLE(res);
    }
    
    return 0.f;
}
Beispiel #13
0
JS_CLASS_METHOD(JsSceneBinding,Loop) {
	if (argc == 1) {
		jsval *args = JS_ARGV(context, vp);
		JSObject *thisObj = JS_THIS_OBJECT(context,vp);
		JsScene *pScene = static_cast<JsScene*>(JS_GetPrivate(context,thisObj));
		if(pScene) {
			pScene->Loop(JSVAL_TO_DOUBLE(args[0]),thisObj);
		}
	}
	return JS_TRUE;
}
Beispiel #14
0
js_compare_atom_keys(const void *k1, const void *k2)
{
    jsval v1, v2;

    v1 = (jsval)k1, v2 = (jsval)k2;
    if (JSVAL_IS_STRING(v1) && JSVAL_IS_STRING(v2))
        return !js_CompareStrings(JSVAL_TO_STRING(v1), JSVAL_TO_STRING(v2));
    if (JSVAL_IS_DOUBLE(v1) && JSVAL_IS_DOUBLE(v2)) {
        double d1 = *JSVAL_TO_DOUBLE(v1);
        double d2 = *JSVAL_TO_DOUBLE(v2);
        if (JSDOUBLE_IS_NaN(d1))
            return JSDOUBLE_IS_NaN(d2);
#ifdef XP_PC
        /* XXX MSVC miscompiles such that (NaN == 0) */
        if (JSDOUBLE_IS_NaN(d2))
            return JS_FALSE;
#endif
        return d1 == d2;
    }
    return v1 == v2;
}
bool TraceMonkeyValue::compare(ScriptValuePtr other)
{
    assert(isValid());
    assert(other->isValid());

    jsval otherValue = (dynamic_cast<TraceMonkeyValue*>(other.get()))->value;

    if (JSVAL_IS_INT(value))
        return (JSVAL_IS_INT(otherValue) && JSVAL_TO_INT(value) == JSVAL_TO_INT(otherValue));
    else if (JSVAL_IS_NULL(value))
        return (JSVAL_IS_NULL(otherValue));
    else if (JSVAL_IS_VOID(value))
        return (JSVAL_IS_VOID(otherValue));
    else if (JSVAL_IS_BOOLEAN(value))
        return (JSVAL_IS_BOOLEAN(otherValue) && JSVAL_TO_BOOLEAN(value) == JSVAL_TO_BOOLEAN(otherValue));
    else if (JSVAL_IS_DOUBLE(value))
        return (JSVAL_IS_DOUBLE(otherValue) && *(JSVAL_TO_DOUBLE(value)) == *(JSVAL_TO_DOUBLE(otherValue)));
    else if (JSVAL_IS_STRING(value))
    {
        if (!JSVAL_IS_STRING(otherValue))
            return false;
        else
            assert(0 && "TODO: compare TM strings");
    }
    else if (JSVAL_IS_OBJECT(value))
    {
        if (JSVAL_IS_NULL(otherValue)) // NULLs are objects, in TraceMonkey
            return false;
        if (!JSVAL_IS_OBJECT(otherValue))
            return false;
        else
            assert(0 && "TODO: compare TM objects");
    }
    else {
        Logging::log(Logging::INFO, "Can't compare uncertain jsvals\r\n");
    }
    assert(0);
    return false;
}
Beispiel #16
0
//
// Native method FileModDateChanged
//
JSBool PR_CALLBACK
InstallFileOpFileModDateChanged(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
  nsInstall *nativeThis =
    GetNativeThis(cx, obj, argv);
  if (!nativeThis)
    return JS_FALSE;

  PRInt32      nativeRet;
  JSObject *jsObj;
  nsInstallFolder *folder;

  *rval = BOOLEAN_TO_JSVAL(PR_FALSE);

  if(argc >= 2)
  {
    //  public int FileModDateChanged (nsInstallFolder aSourceFolder,
    //                                 Number aOldDate);

    jsdouble dval = *(JSVAL_TO_DOUBLE(argv[1]));
    if (argv[0] == JSVAL_NULL || !JSVAL_IS_OBJECT(argv[0])) //argv[0] MUST be a jsval
    {
      *rval =     *rval = BOOLEAN_TO_JSVAL(PR_FALSE);
      return JS_TRUE;
    }

    jsObj = JSVAL_TO_OBJECT(argv[0]);

    if (!JS_InstanceOf(cx, jsObj, &FileSpecObjectClass, nsnull))
    {
      *rval = BOOLEAN_TO_JSVAL(PR_FALSE);
      return JS_TRUE;
    }

    folder = (nsInstallFolder*)JS_GetPrivate(cx, jsObj);

    if(!folder || NS_OK != nativeThis->FileOpFileModDateChanged(*folder, dval, &nativeRet))
    {
      return JS_TRUE;
    }

    *rval = BOOLEAN_TO_JSVAL(nativeRet);
  }
  else
  {
    JS_ReportError(cx, "Function FileModDateChanged requires 2 parameters");
    return JS_TRUE;
  }

  return JS_TRUE;
}
Beispiel #17
0
bool
CoerceDouble(jsval v, double* d)
{
    if (JSVAL_IS_DOUBLE(v)) {
        *d = JSVAL_TO_DOUBLE(v);
    } else if (JSVAL_IS_INT(v)) {
        *d = double(JSVAL_TO_INT(v));
    } else if (JSVAL_IS_VOID(v)) {
        *d = 0.0;
    } else {
        return false;
    }
    return true;
}
Beispiel #18
0
JSAtom *
js_AtomizeValue(JSContext *cx, jsval value, uintN flags)
{
    if (JSVAL_IS_STRING(value))
        return js_AtomizeString(cx, JSVAL_TO_STRING(value), flags);
    if (JSVAL_IS_INT(value))
        return js_AtomizeInt(cx, JSVAL_TO_INT(value), flags);
    if (JSVAL_IS_DOUBLE(value))
        return js_AtomizeDouble(cx, *JSVAL_TO_DOUBLE(value), flags);
    if (JSVAL_IS_OBJECT(value))
        return js_AtomizeObject(cx, JSVAL_TO_OBJECT(value), flags);
    if (JSVAL_IS_BOOLEAN(value))
        return js_AtomizeBoolean(cx, JSVAL_TO_BOOLEAN(value), flags);
    return js_AtomizeHashedKey(cx, value, (JSHashNumber)value, flags);
}
Beispiel #19
0
SEXP
js_to_R(SEXP ptr)
{
    jsval *p = R_ExternalPtrAddr(ptr);
    if(JSVAL_IS_INT(*p))
	return(ScalarInteger(JSVAL_TO_INT(*p)));
    else if(JSVAL_IS_BOOLEAN(*p))
        return(ScalarLogical(JSVAL_TO_BOOLEAN(*p)));
    else if(JSVAL_IS_DOUBLE(*p))
        return(ScalarReal( * JSVAL_TO_DOUBLE(*p)));
    else  if(JSVAL_IS_STRING(*p)) {
	return(jsval_to_string(ptr));
    } else
	return(R_NilValue);
}
Beispiel #20
0
VALUE convert_to_ruby(JohnsonRuntime* runtime, jsval js)
{
  if (JSVAL_NULL == js) return Qnil;

  JSContext * context = johnson_get_current_context(runtime);

  PREPARE_RUBY_JROOTS(context, 1);
  JROOT(js);
  
  switch (JS_TypeOfValue(context, js))
  {
    case JSTYPE_VOID:
      JRETURN_RUBY(Qnil);
      
    case JSTYPE_FUNCTION: 
    case JSTYPE_OBJECT:
      if (OBJECT_TO_JSVAL(runtime->global) == js)
        // global gets special treatment, since the Prelude might not be loaded
        JRETURN_RUBY(make_ruby_land_proxy(runtime, js, "GlobalProxy"));
      
      // this conditional requires the Prelude
      if (js_value_is_symbol(runtime, js))
        JRETURN_RUBY(ID2SYM(rb_intern(JS_GetStringBytes(JS_ValueToString(context, js)))));
    
      if (js_value_is_proxy(runtime, js))
        JRETURN_RUBY(unwrap_js_land_proxy(runtime, js));

      if (js_value_is_regexp(runtime, js))
        JRETURN_RUBY(convert_regexp_to_ruby(runtime, js));
    
      JRETURN_RUBY(make_ruby_land_proxy(runtime, js, "RubyLandProxy"));
        
    case JSTYPE_BOOLEAN:
      JRETURN_RUBY(JSVAL_TRUE == js ? Qtrue : Qfalse);
      
    case JSTYPE_STRING:
      JRETURN_RUBY(convert_js_string_to_ruby(runtime, JSVAL_TO_STRING(js)));
      
    case JSTYPE_NUMBER:
      if (JSVAL_IS_INT(js)) JRETURN_RUBY(INT2FIX(JSVAL_TO_INT(js)));
      else JRETURN_RUBY(rb_float_new(*JSVAL_TO_DOUBLE(js)));

    default:
      JERROR("unknown js type in switch");
  }
  
  JRETURN_RUBY(Qnil);
}
double TraceMonkeyValue::getPropertyFloat(std::string propertyName)
{
    assert(isValid());

    Logging::log(Logging::DEBUG, "TraceMonkeyValue::getPropertyFloat(%s): \r\n", propertyName.c_str()); printJSVAL(value);

    assert(JSVAL_IS_OBJECT(value));
    jsval ret;
    bool success = JS_GetProperty(TraceMonkeyEngine::context, JSVAL_TO_OBJECT(value), propertyName.c_str(), &ret);
    assert(success);
    assert(JSVAL_IS_DOUBLE(ret));

    Logging::log(Logging::DEBUG, "returning: \r\n"); printJSVAL(ret);

    return *(JSVAL_TO_DOUBLE(ret));
}
Beispiel #22
0
JSBool
js_ValueToBoolean(JSContext *cx, jsval v, JSBool *bp)
{
    JSBool b;
    jsdouble d;

    if (JSVAL_IS_NULL(v) || JSVAL_IS_VOID(v))
    {
        b = JS_FALSE;
    }
    else if (JSVAL_IS_OBJECT(v))
    {
        if (!JS_VERSION_IS_ECMA(cx))
        {
            if (!OBJ_DEFAULT_VALUE(cx, JSVAL_TO_OBJECT(v), JSTYPE_BOOLEAN, &v))
                return JS_FALSE;
            if (!JSVAL_IS_BOOLEAN(v))
                v = JSVAL_TRUE; /* non-null object is true */
            b = JSVAL_TO_BOOLEAN(v);
        }
        else
        {
            b = JS_TRUE;
        }
    }
    else if (JSVAL_IS_STRING(v))
    {
        b = JSSTRING_LENGTH(JSVAL_TO_STRING(v)) ? JS_TRUE : JS_FALSE;
    }
    else if (JSVAL_IS_INT(v))
    {
        b = JSVAL_TO_INT(v) ? JS_TRUE : JS_FALSE;
    }
    else if (JSVAL_IS_DOUBLE(v))
    {
        d = *JSVAL_TO_DOUBLE(v);
        b = (!JSDOUBLE_IS_NaN(d) && d != 0) ? JS_TRUE : JS_FALSE;
    }
    else
    {
        JS_ASSERT(JSVAL_IS_BOOLEAN(v));
        b = JSVAL_TO_BOOLEAN(v);
    }

    *bp = b;
    return JS_TRUE;
}
Beispiel #23
0
/*
 * Log a given jsval with a prefix.
 *  cx - JSContext for the JS execution context to use
 *  val - jsval to print
 *  prefix - string to print before the value, defaults to empty string
 *
 * TODO(jat): this whole printf-style logging needs to be replaced, but we
 * run into library version issues if we use C++ iostreams so we would need
 * to implement our own equivalent.  Given that this code is all likely to
 * be rewritten for out-of-process hosted mode, it seems unlikely to be worth
 * the effort until that is completed.
 */
void PrintJSValue(JSContext* cx, jsval val, char* prefix="") {
    JSType type = JS_TypeOfValue(cx, val);
    const char* typeString=JS_GetTypeName(cx, type);
    static const int BUF_SIZE = 256;
    char buf[BUF_SIZE];
    const char *bufEnd = buf + BUF_SIZE;
    char* p = buf;
    p += append_sprintf(p, bufEnd, "%s%s", prefix, typeString);
    switch(type) {
    case JSTYPE_VOID:
        break;
    case JSTYPE_BOOLEAN:
        p += append_sprintf(p, bufEnd, ": %s",
                            JSVAL_TO_BOOLEAN(val) ? "true" : "false");
        break;
    case JSTYPE_NUMBER:
        if (JSVAL_IS_INT(val)) {
            p += append_sprintf(p, bufEnd, ": %d", JSVAL_TO_INT(val));
        } else {
            p += append_sprintf(p, bufEnd, ": %lf", (double)*JSVAL_TO_DOUBLE(val));
        }
        break;
    case JSTYPE_OBJECT: {
        JSObject* obj = JSVAL_TO_OBJECT(val);
        if (!JSVAL_IS_OBJECT(val)) break;
        JSClass* clazz = obj ? JS_GET_CLASS(cx, obj) : 0;
        p += append_sprintf(p, bufEnd, " @ %08x, class %s",
                            (unsigned)obj, clazz ? clazz->name : "<null>");
        break;
    }
    case JSTYPE_FUNCTION:
    case JSTYPE_LIMIT:
        break;
    case JSTYPE_STRING: {
        /*
         * TODO(jat): support JS strings with international characters
         */
        JsStringWrap str(cx, JSVAL_TO_STRING(val));
        p += append_sprintf(p, bufEnd, ": %.*s", str.length(), str.bytes());
        break;
    }
    }
    Tracer::log("%s", buf);
}
nsIVariant *
convertJSValToVariant(
  JSContext *aCtx,
  jsval aValue)
{
  if (JSVAL_IS_INT(aValue))
    return new IntegerVariant(JSVAL_TO_INT(aValue));

  if (JSVAL_IS_DOUBLE(aValue))
    return new FloatVariant(JSVAL_TO_DOUBLE(aValue));

  if (JSVAL_IS_STRING(aValue)) {
    JSString *str = JSVAL_TO_STRING(aValue);
    nsDependentJSString value;
    if (!value.init(aCtx, str))
        return nsnull;
    return new TextVariant(value);
  }

  if (JSVAL_IS_BOOLEAN(aValue))
    return new IntegerVariant((aValue == JSVAL_TRUE) ? 1 : 0);

  if (JSVAL_IS_NULL(aValue))
    return new NullVariant();

  if (JSVAL_IS_OBJECT(aValue)) {
    JSObject *obj = JSVAL_TO_OBJECT(aValue);
    // We only support Date instances, all others fail.
    if (!::js_DateIsValid(aCtx, obj))
      return nsnull;

    double msecd = ::js_DateGetMsecSinceEpoch(aCtx, obj);
    msecd *= 1000.0;
    PRInt64 msec;
    LL_D2L(msec, msecd);

    return new IntegerVariant(msec);
  }

  return nsnull;
}
static void
ValueToString(JSContext *cx, jsval aJSValue, nsString& aString)
{
	if (JSVAL_IS_VOID(aJSValue)) {
		aString.Assign(kUndefined, STRLEN_ARRAY(kUndefined));
	} else if (JSVAL_IS_NULL(aJSValue)) {
		aString.Assign(kNull, STRLEN_ARRAY(kNull));
	} else if (JSVAL_IS_INT(aJSValue)) {
		jsint i = JSVAL_TO_INT(aJSValue);
		char buf[20];
		PR_snprintf(buf, sizeof(buf), "%d", i);
		aString.Assign(NS_ConvertASCIItoUTF16(buf));
	} else if (JSVAL_IS_DOUBLE(aJSValue)) {
		jsdouble d = JSVAL_TO_DOUBLE(aJSValue);
		char buf[50];
		PR_snprintf(buf, sizeof(buf), "%f", d);
		aString.Assign(NS_ConvertASCIItoUTF16(buf));
	} else if (JSVAL_IS_BOOLEAN(aJSValue)) {
		JSBool b = JSVAL_TO_BOOLEAN(aJSValue);
		if (b)
			aString.Assign(kTrue, STRLEN_ARRAY(kTrue));
		else
			aString.Assign(kFalse, STRLEN_ARRAY(kFalse));
	} else if (JSVAL_IS_STRING(aJSValue)) {
		JSString *str = JSVAL_TO_STRING(aJSValue);
		size_t len;
		const jschar *chars = JS_GetStringCharsAndLength(cx, str, &len);
		NS_ASSERTION(chars, "out of memory");
		if (chars) {
			NS_ASSERTION(sizeof(jschar) == sizeof(PRUnichar),
						 "char size mismatch");
			aString.Assign(reinterpret_cast<const PRUnichar*>(chars), len);
		}
	} else {
		JSObject *obj = JSVAL_TO_OBJECT(aJSValue);
		JSClass *clazz = JS_GetClass(cx, obj);
		aString.Assign(PRUnichar('['));
		aString.Append(NS_ConvertASCIItoUTF16(clazz->name));
		aString.Append(PRUnichar(']'));
	}
}
Beispiel #26
0
JSBool
js_AtomizePrimitiveValue(JSContext *cx, jsval v, JSAtom **atomp)
{
    JSAtom *atom;

    if (JSVAL_IS_STRING(v)) {
        atom = js_AtomizeString(cx, JSVAL_TO_STRING(v), 0);
        if (!atom)
            return JS_FALSE;
    } else if (JSVAL_IS_DOUBLE(v)) {
        atom = js_AtomizeDouble(cx, *JSVAL_TO_DOUBLE(v));
        if (!atom)
            return JS_FALSE;
    } else {
        JS_ASSERT(JSVAL_IS_INT(v) || JSVAL_IS_BOOLEAN(v) ||
                  JSVAL_IS_NULL(v) || JSVAL_IS_VOID(v));
        atom = (JSAtom *)v;
    }
    *atomp = atom;
    return JS_TRUE;
}
Beispiel #27
0
js_hash_atom_key(const void *key)
{
    jsval v;
    jsdouble *dp;

    /* Order JSVAL_IS_* tests by likelihood of success. */
    v = (jsval)key;
    if (JSVAL_IS_STRING(v))
        return js_HashString(JSVAL_TO_STRING(v));
    if (JSVAL_IS_INT(v))
        return HASH_INT(JSVAL_TO_INT(v));
    if (JSVAL_IS_DOUBLE(v)) {
        dp = JSVAL_TO_DOUBLE(v);
        return HASH_DOUBLE(dp);
    }
    if (JSVAL_IS_OBJECT(v))
        return HASH_OBJECT(JSVAL_TO_OBJECT(v));
    if (JSVAL_IS_BOOLEAN(v))
        return HASH_BOOLEAN(JSVAL_TO_BOOLEAN(v));
    return (JSHashNumber)v;
}
//---------------------------------------------------------------------------
void JS_TO_CONCEPT(void *member, jsval rval) {
    INVOKE_CALL            InvokePtr   = 0;
    CALL_BACK_VARIABLE_SET SetVariable = _SetVariable;
    void *HANDLER = CONCEPT_HANDLER;

    if (JSVAL_IS_DOUBLE(rval)) {
        SET_NUMBER_VARIABLE(member, *JSVAL_TO_DOUBLE(rval));
    } else
    if (JSVAL_IS_INT(rval)) {
        SET_NUMBER_VARIABLE(member, JSVAL_TO_INT(rval));
    } else
    if (JSVAL_IS_STRING(rval)) {
        struct JSString *str = JSVAL_TO_STRING(rval);
        SET_BUFFER_VARIABLE(member, JS_GetStringBytes(str), str->length);
    } else
    if (JSVAL_IS_BOOLEAN(rval)) {
        SET_NUMBER_VARIABLE(member, (NUMBER)JSVAL_TO_BOOLEAN(rval));
    } else
    if ((JSVAL_IS_NULL(rval)) || (JSVAL_IS_VOID(rval))) {
        SET_NUMBER_VARIABLE(member, 0);
    } else
        SET_NUMBER_VARIABLE(member, 1);
}
Beispiel #29
0
SV *
PrimJSVALToSV(
    pTHX_
    JSContext *cx,
    jsval v
) {
    SV *sv = NULL;
    if(JSVAL_IS_NULL(v) || JSVAL_IS_VOID(v))
	sv = newSV(0);
    else if(JSVAL_IS_BOOLEAN(v)) {
	sv = newSV(0);
	sv_setref_iv(sv, PJS_BOOLEAN, (IV)JSVAL_TO_BOOLEAN(v));
    }
    else if(JSVAL_IS_INT(v)) 
	sv = newSViv((IV)JSVAL_TO_INT(v));
    else if(JSVAL_IS_DOUBLE(v))
	sv = newSVnv(*JSVAL_TO_DOUBLE(v));
    else if(JSVAL_IS_STRING(v)) 
	sv = PJS_JSString2SV(aTHX_ JSVAL_TO_STRING(v));
    else croak("PJS_Assert: Unknown primitive type");
    
    return sv;
}
void printJSVAL(jsval value)
{
    if (JSVAL_IS_INT(value))
        Logging::log(Logging::INFO, "INT: %d\r\n", JSVAL_TO_INT(value));
    else if (JSVAL_IS_NULL(value))
        Logging::log(Logging::INFO, "NULL (null)\r\n");
    else if (JSVAL_IS_VOID(value))
        Logging::log(Logging::INFO, "VOID (undefined)\r\n");
    else if (JSVAL_IS_BOOLEAN(value))
        Logging::log(Logging::INFO, "BOOLEAN: %d\r\n", JSVAL_TO_BOOLEAN(value));
    else if (JSVAL_IS_NUMBER(value))
        Logging::log(Logging::INFO, "NUMBER: %f\r\n", *JSVAL_TO_DOUBLE(value));
    else if (JSVAL_IS_DOUBLE(value))
        Logging::log(Logging::INFO, "DOUBLE: %f\r\n", *JSVAL_TO_DOUBLE(value));
    else if (JSVAL_IS_STRING(value))
    {
        JSString* str = JS_ValueToString(TraceMonkeyEngine::context, value);
        char* chr = JS_GetStringBytes(str);
        Logging::log(Logging::INFO, "STRING: %s\r\n", chr);

/*
        INDENT_LOG(Logging::DEBUG);

        jsval func;
        bool success = JS_GetProperty(TraceMonkeyEngine::context, TraceMonkeyEngine::global, "log", &func);
        assert(success);

        assert(JSVAL_IS_OBJECT(func));
        assert(!JSVAL_IS_NULL(func));

        jsval jsArgs[2];
        jsArgs[0] = INT_TO_JSVAL(Logging::DEBUG);
        jsArgs[1] = value;

        jsval ret;

        JS_CallFunctionValue(TraceMonkeyEngine::context, JSVAL_TO_OBJECT(value), func, 2, jsArgs, &ret);
*/
    }
    else if (JSVAL_IS_OBJECT(value))
    {
        Logging::log(Logging::INFO, "OBJECT (object)\r\n");

        assert(!JSVAL_IS_NULL(value));
        jsval ret;
        JS_GetProperty(TraceMonkeyEngine::context, JSVAL_TO_OBJECT(value), "uniqueId", &ret);

        if (JSVAL_IS_INT(ret))
            Logging::log(Logging::INFO, "OBJECT (object) has uniqueId: %d\r\n", JSVAL_TO_INT(ret));
        else
            Logging::log(Logging::INFO, "OBJECT (object) has no uniqueId\r\n");

/*
        if (TraceMonkeyEngine::global)
        {
            ScriptEngineManager::getGlobal()->call("log",
                ScriptValueArgs().append(Logging::DEBUG).append(
//                    ScriptEngineManager::getGlobal()->call("serializeJSON",
                        ScriptEngineManager::getGlobal()->call("keys", // Just print keys, to avoid recursions etc.
                            ScriptValuePtr(new TraceMonkeyValue(true, value))
                        )
//                    )
                )
            );
        }
*/
    }
    else {
        Logging::log(Logging::INFO, "Uncertain jsval\r\n");
    }
}