-
-
Notifications
You must be signed in to change notification settings - Fork 184
Generalize class accessors transform #774
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Perryvw
merged 11 commits into
TypeScriptToLua:master
from
ark120202:generalize-class-accessors
Mar 12, 2020
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
bf676ee
Generalize class accessors transform
ark120202 27b931a
Remove `rawset` from the list of used builtins
ark120202 5921a8d
Fix sourcemaps
ark120202 f677258
Improve class extends test case
ark120202 77bf031
Improve lualib types
ark120202 1851195
Add few comments to lualib
ark120202 3b81007
Temporary revert getter property override removal
ark120202 72eec92
Merge remote-tracking branch 'upstream/master' into generalize-class-…
ark120202 f10e2fe
Add changelog
ark120202 03489ad
Change sourcemap class extends mapping pattern
ark120202 e224539
Merge branch 'master' into generalize-class-accessors
ark120202 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| function __TS__ClassExtends(this: void, target: LuaClass, base: LuaClass): void { | ||
| target.____super = base; | ||
|
|
||
| // Set base class as a metatable, because descriptors use `getmetatable` to get extended prototype | ||
| const staticMetatable: any = setmetatable({ __index: base }, base); | ||
| setmetatable(target, staticMetatable); | ||
|
|
||
| const baseMetatable = getmetatable(base); | ||
| if (baseMetatable) { | ||
| // Re-add metatable events defined by descriptors | ||
| if (typeof baseMetatable.__index === "function") staticMetatable.__index = baseMetatable.__index; | ||
| if (typeof baseMetatable.__newindex === "function") staticMetatable.__newindex = baseMetatable.__newindex; | ||
| } | ||
|
|
||
| setmetatable(target.prototype, base.prototype); | ||
| // Re-add metatable events defined by accessors with `__TS__SetDescriptor` | ||
| if (typeof base.prototype.__index === "function") target.prototype.__index = base.prototype.__index; | ||
| if (typeof base.prototype.__newindex === "function") target.prototype.__newindex = base.prototype.__newindex; | ||
| } |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| function ____descriptorIndex(this: any, key: string): void { | ||
| const value = rawget(this, key); | ||
| if (value !== null) { | ||
| return value; | ||
| } | ||
|
|
||
| let metatable = getmetatable(this); | ||
| while (metatable) { | ||
| const rawResult = rawget(metatable, key); | ||
| if (rawResult !== undefined) { | ||
| return rawResult; | ||
| } | ||
|
|
||
| const descriptors = rawget(metatable, "_descriptors"); | ||
| if (descriptors) { | ||
| const descriptor: PropertyDescriptor = descriptors[key]; | ||
| if (descriptor) { | ||
| if (descriptor.get) { | ||
| return descriptor.get.call(this); | ||
| } | ||
|
|
||
| return; | ||
| } | ||
| } | ||
|
|
||
| metatable = getmetatable(metatable); | ||
| } | ||
| } | ||
|
|
||
| function ____descriptorNewindex(this: any, key: string, value: any): void { | ||
| let metatable = getmetatable(this); | ||
| while (metatable) { | ||
| const descriptors = rawget(metatable, "_descriptors"); | ||
| if (descriptors) { | ||
| const descriptor: PropertyDescriptor = descriptors[key]; | ||
| if (descriptor) { | ||
| if (descriptor.set) { | ||
| descriptor.set.call(this, value); | ||
| } | ||
|
|
||
| return; | ||
| } | ||
| } | ||
|
|
||
| metatable = getmetatable(metatable); | ||
| } | ||
|
|
||
| rawset(this, key, value); | ||
| } | ||
|
|
||
| // It's also used directly in class transform to add descriptors to the prototype | ||
| function __TS__SetDescriptor(this: void, metatable: Metatable, prop: string, descriptor: PropertyDescriptor): void { | ||
| if (!metatable._descriptors) metatable._descriptors = {}; | ||
| metatable._descriptors[prop] = descriptor; | ||
|
|
||
| if (descriptor.get) metatable.__index = ____descriptorIndex; | ||
| if (descriptor.set) metatable.__newindex = ____descriptorNewindex; | ||
| } | ||
|
|
||
| // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty | ||
| function __TS__ObjectDefineProperty<T extends object>( | ||
| this: void, | ||
| object: T, | ||
| prop: string, | ||
| descriptor: PropertyDescriptor | ||
| ): T { | ||
| let metatable = getmetatable(object); | ||
| if (!metatable) { | ||
| metatable = {}; | ||
| setmetatable(object, metatable); | ||
| } | ||
|
|
||
| __TS__SetDescriptor(metatable, prop, descriptor); | ||
| return object; | ||
| } | ||
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.