create blog

go home go home
  1. about
  2. code
  3. wiki
  4. blog

Archive for January, 2009

The Script Engine

Saturday, January 31st, 2009

One of the components of the framework on which I have been working particularly hard in the past week is the Script Engine.

It is what it sounds like: a module designed to allow scripting to work inside the framework. Although most definitely still a work-in-progress, it now has a lot of the features I want it to have.

My goals were:

  • It should be extremely easy to make an object scriptable (less than ten minutes)
  • It should be extremely easy to pass objects to JavaScript
  • It should be extremely easy to retrieve objects from JavaScript
  • It should be possible to implement other scripting languages in future

I don’t know about the last, as I haven’t tried it yet, but the first three I think I may have succeeded in to some extent.

Basically, to give you the idea of what it does, I’m going to give a code dump showing how the script engine works. You very likely will not be interested. On the other hand, you could. So, I am posting it.

(more…)

Persistent Handles

Saturday, January 31st, 2009

You probably don’t like memory leaks. I don’t. But, if you have one object that needs to pass between two languages — C++ and JavaScript — each of which may keep that object indefinitely, it can become tricky to make sure that any given object is destroyed, and that it is destroyed at the right time.

There are two things that can happen: an object can be deleted two soon, while it is still being used elsewhere — causing some particularly delicious crashes — or, an object can be deleted too late (or not at all), causing memory leaks.

(more…)

V8 Objects

Thursday, January 29th, 2009

So, a problem arose.

I wanted to account for two possibilities in the Script Engine: Passing existing objects from C++ to JavaScript, and creating new C++ objects from JavaScript. The act of passing a native object into scripting languages is usually referred to as wrapping (although, at one point inside the engine, I actually refer to a part of the process as unwrapping).

The code I would like to be able to use is:

//create a point
Point *point = new Point(.42, 42);
 
//pass it to some JavaScript function
myJavaScriptFunction->Call(pointWrap(point), 0, NULL);
//create a Point
var point = new Point();
 
//set its X value
point.x = 4.2;
 
//call some C++ function using it
myCPPFunction(point);

So how did I manage this in the engine? Well, as I am prone do, I overcomplicated it.

(more…)

Welcome!

Wednesday, January 28th, 2009

It is, I believe, customary to have a welcome post. Please consider this humble little post to be your welcome. If you find it unsatisfactory, just pretend something satisfactory is here. Or look at the first real post.

Thank you.