This project has moved and is read-only. For the latest updates, please go here.

Object with properties

Feb 17, 2012 at 3:09 PM

Hello, I have Leaderboard class that inherits ObjectInstance. I have function get(int position) in it.

This function should return object with two parameters - username (string) and time (double). I tried many different ways but I still can't realize how to do that.

Could anyone help me?

Feb 17, 2012 at 7:34 PM
This can be done fairly simple. You can for instance create a new ObjectInstance and populate it (something like `var ret = new ObjectInstance(Engine); ret["username"] = username; ret["time"] = time; return ret;`), or you can also (if you rather want) create a JSON string like so: {"username":"<username_here>","time":<time_here>} and then pass that to the JSON deserializer built in to Jurassic.

Last, but not least you can create a new subclass of ObjectInstance that has these properties, and return a new instance of that, so lots and lots to chose from :).
Feb 17, 2012 at 9:42 PM

Oh, thanks for fast reply but I have a problem again. :(

var ret = new ObjectInstance(Engine); doesn't work at all (while hint is seen in Visual Studio!) "Jurassic.Library.ObjectInstance" does not contain a constructor that takes '0' arguments. What the hell?

I tried the third way - made class that inherits ObjectInstance with username and time parameters but it also doesn't work and the reason is that "TypeError: undefined cannot be casted to object." (something like that, I don't remember definitely now)

What to do? Sorry for my impatience and noobness. I really seem to be unlucky.

Feb 18, 2012 at 7:35 AM

All ObjectInstance constructors are protected, but you can create a new object like so:

var obj = engine.Object.Construct();

 (where engine is of type Jurassic.ScriptEngine)

Feb 18, 2012 at 9:59 AM

Okay, this works. But I have something wrong with this:

public static ObjectInstance Get(int num)
if (num < p.results.Count) return null;
if (num <= 0 || num > p.results.Count) return null;
string[] info = p.results[num - 1].Split(' ');
var obj = p.engine.Object.Construct();
obj["username"] = info[0];
obj["time"] = Double.Parse(info[1]);
 return obj;

Am I doing this right? It seems that no because I get an exception (JavaScript exception) here:

try {
var ldr = leaderboard.get(1);
} catch(exc) {
log('exception '+ exc);

Exception text is all the same: TypeError: undefined cannot be converted to an object.

Thanks for you fast replies, Jurassic is the best. :)

Feb 18, 2012 at 12:31 PM
Edited Feb 18, 2012 at 12:35 PM

Your method always returns null, which is converted to undefined when it is passed back into JavaScript land.

(Hint: check the very first line of the .NET method)

EDIT: I'm wrong, there is a small possibility of the method returning non-null, if num=p.results.Count and p.results.Count>0.

Feb 18, 2012 at 2:33 PM

Thank you! 

I also had little problem with p.engine. I replaced it with real engine (= Engine).

Feb 19, 2012 at 3:09 AM
Speaking of which paul; I've had problems with js null turning into "null" (string). Is this normall? It's at least isn't very intuetive.

I had a method where I did if(!string.IsNullOrEmpty(str)), however, becuase of this I was forced to add if(str == "null") str = null; first. Also, this means it's impossible to pass in the actual string "null". If you ask me, both null and undefined should map to null when you call a .NET function from JS.
Feb 19, 2012 at 10:09 AM

Jurassic uses the standard ECMAScript type conversion rules when converting function parameters (as implemented in TypeConverter and EmitConversion).  An example of where this is used is when adding a value to a string (e.g. null + "", undefined + "").  Calling a .NET method obviously is not part of the standard, but .NET methods are used to implement the standard library, and a lot of standard library functions rely on this behavior.  For example, "test".concat(undefined) gives "testundefined".

By the way, if you need to distinguish between "null" and null, you can declare your parameter as an ObjectInstance and check for Null.Value, then convert as needed using TypeConverter.  (You can use the same technique for undefined).