Skip to content

Classes update#383

Merged
tomblind merged 7 commits intomasterfrom
classes-update
Feb 11, 2019
Merged

Classes update#383
tomblind merged 7 commits intomasterfrom
classes-update

Conversation

@tomblind
Copy link
Copy Markdown
Collaborator

@tomblind tomblind commented Feb 8, 2019

fixes #256
fixes #351
fixes #353

This PR reworks classes to use separate static and instance tables. It also adds errors for mis-uses of extension and metaExtension decorated classes.

Some notes:

  • The instance table is assigned to "ClassName.prototype", and that table has a reference back through a "constructor" property. This is compatible with how JS classes are structured.
  • The "new" and "constructor" functions were renamed with leading underscores to prevent potential name collisions
  • "__base" was renamed to "____super" for clarity
  • "super" method and constructor calls now have two ways they can be transpiled. A short version: "BaseClass.prototype", which is the default, and a long version: "DerivedClass.____super.prototype" which is used when a class extends from something other than a simple identifier ("class Derived extends makeBase() {}")

Basic Example:

class Foo {
    prop = "prop";
    constructor() {}
    method() {}
    static staticProp = "staticProp";
    static staticMethod() {}
}
const foo = new Foo();
Foo = Foo or {};
Foo.__index = Foo;
Foo.prototype = Foo.prototype or {};
Foo.prototype.__index = Foo.prototype;
Foo.prototype.constructor = Foo;
Foo.new = function(...)
    local self = setmetatable({}, Foo.prototype);
    self:____constructor(...);
    return self;
end;
Foo.staticProp = "staticProp";
Foo.prototype.____constructor = function(self)
    self.prop = "prop";
end;
Foo.prototype.method = function(self)
end;
Foo.staticMethod = function(self)
end;
local foo = Foo.new();

Sub-class example:

class Bar extends Foo {
    constructor() { super(); }
    method() { super.method(); }
}
const bar = new Bar();
Bar = Bar or {};
Bar.__index = Bar;
Bar.prototype = Bar.prototype or {};
Bar.prototype.__index = Bar.prototype;
Bar.prototype.constructor = Bar;
Bar.____super = Foo;
setmetatable(Bar, Bar.____super);
setmetatable(Bar.prototype, Bar.____super.prototype);
Bar.new = function(...)
    local self = setmetatable({}, Bar.prototype);
    self:____constructor(...);
    return self;
end;
Bar.prototype.____constructor = function(self)
    Foo.prototype.____constructor(self);
end;
Bar.prototype.method = function(self)
    Foo.prototype.method(self);
end;
local bar = Bar.new();

- classes separated into static and instance tables
- fixed issues with super calls
- renamed new and constructor to avoid name collisions
- added errors for extensions being used in inappropriate ways
...and using it to prevent the same nodes being referenced from different places in the lua ast
@tomblind tomblind requested a review from lolleko February 9, 2019 17:55
@tomblind tomblind merged commit 03beabb into master Feb 11, 2019
@tomblind tomblind deleted the classes-update branch February 11, 2019 12:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants