Subj : Re: Returning integers from custom functions. To : Frank Schroeder From : Brendan Eich Date : Fri Jul 18 2003 07:11 pm Frank Schroeder wrote: >I've implemented a custom object with a couple of functions that are >supposed to return an integer value: > > >static JSBool Input_Width(JSContext *cx, JSObject *obj, uintN argc, jsval >*argv, jsval *rval) >{ > *rval=STRING_TO_JSVAL(JS_NewStringCopyZ(cx, "53")); > > return JS_TRUE; >} > > Don't return a string if you want to return a number. Also, if you do call JS_NewStringCopyZ, check for failure (null return value). >When I call the function and perform an addition, > >Response.Write(Input.Width()+1); > >the result is 531, so it's concatenating the strings. Weird enough, >subtraction etc. is working fine, i.e. > >Response.Write(Input.Width()-1); > >comes out as 52. > That's how the language works, and always has. >What can I do to have the return value correcly treated as an integer/double >and perform an integer addition? I've tried replacing the STRING_TO_JSVAL >with DOUBLE_TO_JSVAL, to no avail. > You realize DOUBLE_TO_JSVAL takes a pointer to a jsdouble that was allocated from the GC heap? Sterling suggested: JS_NewNumberValue(cx,53,&rval); which works fine, but if you know you want 53, just rval = INT_TO_JSVAL(53); works the same and is faster, infallible, and less code. /be .