Jump to content

  • Log In with Google      Sign In   
  • Create Account






DeyjaScript 7 : Scope fixes and Inheritance

Posted by Deyja, 23 September 2011 · 192 views

This entry will be formatted as a series of features, and some notes on their implementation.

First, I created a proper scope stack. This allowed me to fix the problem of variables 'leaking out of' blocks. Code like this used to be possible.

if (condition) {
	int x = 5;
}
x = 6; //Just fine!

It takes more work to prevent that sort of thing, but code like that is dangerous enough to warrant it. Lets see what could happen if I allowed it.

if (condition that is false) {
	Foo foo = new Foo();
	//code that uses foo
}
//many more lines of code
foo.doSomething(); //<- Sometimes foo has been initialized, sometimes it hasn't.

If I detected the use of uninitialized variables like C# does, I could make this particular case an error. But it's easier to just prevent leaking. So, each block pushes a scope onto the scope-stack. Statements will search up the scope stack for the variables they reference. When the function scope is closed, it assigns ids to all the local variables used in all it's sub scopes. It doesn't need a unique id for every variable, it only needs to know the maximum number of variables used at once.

In this example, x and y will be given the exact same id.
if (condition) {
	int x;
} else {
	int y;
}

All's not well, though, because a naked statement is not a block. There's no block node involved to push scopes. So if I were to do like so

if (condition)
	int x;

not only would I be accomplishing absolutely nothing, I'd be leaking x into the enclosing scope. Then consider a for statement, which often has a variable declaration in it, outside the block. I would leak that as well. The solution is for these control structures to also push scopes. In the case of an if statement with a block rather than a single statement, two scopes will be pushed. One of them will remain empty, but that's fine, all of these function-level scopes are discarded when function compilation is finished.

Next I added constructors. This was simple. I modify the grammar so that a new statement expects an argument list, and the node looks for a method on the type called 'construct' and that takes those parameter types. If the constructor was found, it emits bytecode to call the method. If it wasn't and there are no arguments, that's fine. If it wasn't found and there are arguments, it's an error. So all objects have a default constructor that does nothing.

The next feature I implemented was enabled by this scope-stack addition, and it was inheritance. Inheritance can be complicated, so I started with some simple restrictions. First, objects can only derive from one base. This means that I don't need to figure out where in an object's memory layout a particular base is at. The base is always first. I also check for and prevent base loops, where B derives from A, and A derives from B. This is only even possible because I allow objects to be declared in any order. Second, all methods are virtual. There is no additional cost for a virtual function since all method calls already involve looking up the function in a table. My particular implementation, however, prevents calling the base type's version of the method (Something I may want to support, particularly for constructors).

First I inherit member variables. Memory layout is a non-issue. Members of the base are just listed first. For example

A {
	int x;
	int y;
}

B is an A {
	int z;
}

Here, in an instance of B, z will have an index of 2. If I convert a B to an A, x and y will still be in the right place; there will just be an extra member hanging on the end. The scope keeps track of how many member variables it has total so that ScriptObject can make enough space for all of them. I modify the scope to look for variables in base types too (Variables with the same name in derived scopes will hide base members) and it works.

Next, I want to inherit functions. This is a little more complicated because, unlike variables, I actually need to be able to lookup the function at runtime. So I still list base member functions first, but I duplicate the function list into the derived scope, and then add the local functions to it. When I find a local function with the same decorated name as a base function, I replace the base function entry. Since the dynamic type of a script object is stored in the script object, and base members are listed first, I can treat a B as an A, call a method from A, and it will have the same index in B's function list.

I think I have enough now to move on. Next time, I'll see about making sure I can call base implementations of virtual functions. It might also be a good idea if not every function was virtual. And then it's time for modules.

Attached Files






July 2014 »

S M T W T F S
  12345
6789101112
13141516171819
202122 23 242526
2728293031  

Recent Comments

Recent Comments

PARTNERS