-
Notifications
You must be signed in to change notification settings - Fork 57
Luals docgen #99
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
Merged
Luals docgen #99
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
3c3e355
proof of concept
VisenDev c3dd769
more types working
VisenDev 1a04340
more types working
VisenDev 65488f8
custom build step now working
VisenDev 84ce7ae
integrated things better
VisenDev c654025
fixed segfaults, still need to add install step
VisenDev 9133013
refactored in new file
VisenDev 3d6b352
fixed memory corruption issues
VisenDev 7455f58
polished implementation
VisenDev 8b4da8b
removed old comments
VisenDev cd0184d
removed old testing code
VisenDev 4704964
reworked define system to support build arguments
VisenDev 7a3a31f
add allocator parameter
VisenDev 6c348cb
properly close file
VisenDev 1c27125
adding debug logging for testing
VisenDev a6de64c
added init capacity
VisenDev 8d53895
added more debugging logs
VisenDev 2a29d99
removed deinit from hashmap for testing
VisenDev a6d8645
allocator experiments
VisenDev a4d69f1
changing id test
VisenDev 572dfac
more changes to how Ids are defined:
VisenDev 44a7d58
reworked how arraylists are stored
VisenDev b4402a9
removed debugging print statements, fixed bug
VisenDev 8f01465
minor testing change
VisenDev 435668b
added more spacing between types
VisenDev b878e3f
added annotations for testing
VisenDev 07a2388
totally reworked implementation to fix bugs
VisenDev 547c0da
allow struct fields with default values to be null
VisenDev 4f6f021
change how optional fields are notated
VisenDev 6ed5166
revert back to old optional field syntax
VisenDev 5275b0a
added unit test
VisenDev 1c8afa7
remove outdated decls
VisenDev 21353cb
remove error union return type from build.zig
VisenDev 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
.zig-cache/ | ||
zig-out/ | ||
definitions.lua |
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,14 @@ | ||
const std = @import("std"); | ||
const ziglua = @import("ziglua"); | ||
|
||
const T = struct { foo: i32 }; | ||
const MyEnum = enum { asdf, fdsa, qwer, rewq }; | ||
const SubType = struct { foo: i32, bar: bool, bip: MyEnum, bap: ?[]MyEnum }; | ||
const Bippity = struct { A: ?i32, B: *bool, C: []const u8, D: ?*SubType }; | ||
const TestType = struct { a: i32, b: f32, c: bool, d: SubType, e: [10]Bippity }; | ||
const Foo = struct { far: MyEnum, near: SubType }; | ||
|
||
pub fn main() !void { | ||
const output_file_path = std.mem.sliceTo(std.os.argv[1], 0); | ||
try ziglua.define(std.heap.c_allocator, output_file_path, &.{ T, TestType, Foo }); | ||
} |
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,169 @@ | ||
const std = @import("std"); | ||
|
||
const String = std.ArrayList(u8); | ||
const Database = std.StringHashMap(void); | ||
|
||
pub const DefineState = struct { | ||
allocator: std.mem.Allocator, | ||
database: Database, | ||
definitions: std.ArrayList(String), | ||
|
||
pub fn init(alloc: std.mem.Allocator) DefineState { | ||
return DefineState{ | ||
.allocator = alloc, | ||
.database = Database.init(alloc), | ||
.definitions = std.ArrayList(String).init(alloc), | ||
}; | ||
} | ||
|
||
pub fn deinit(self: *@This()) void { | ||
for (self.definitions.items) |def| { | ||
def.deinit(); | ||
} | ||
defer self.database.deinit(); | ||
defer self.definitions.deinit(); | ||
} | ||
}; | ||
|
||
pub fn define( | ||
alloc: std.mem.Allocator, | ||
absolute_output_path: []const u8, | ||
comptime to_define: []const type, | ||
) !void { | ||
var state = DefineState.init(alloc); | ||
defer state.deinit(); | ||
|
||
inline for (to_define) |T| { | ||
_ = try addClass(&state, T); | ||
} | ||
|
||
var file = try std.fs.createFileAbsolute(absolute_output_path, .{}); | ||
defer file.close(); | ||
|
||
try file.seekTo(0); | ||
try file.writeAll(file_header); | ||
|
||
for (state.definitions.items) |def| { | ||
try file.writeAll(def.items); | ||
try file.writeAll("\n"); | ||
} | ||
|
||
try file.setEndPos(try file.getPos()); | ||
} | ||
|
||
const file_header: []const u8 = | ||
\\---@meta | ||
\\ | ||
\\--- This is an autogenerated file, | ||
\\--- Do not modify | ||
\\ | ||
\\ | ||
; | ||
|
||
fn name(comptime T: type) []const u8 { | ||
return (comptime std.fs.path.extension(@typeName(T)))[1..]; | ||
} | ||
|
||
fn addEnum( | ||
state: *DefineState, | ||
comptime T: type, | ||
) !void { | ||
if (state.database.contains(@typeName(T)) == false) { | ||
try state.database.put(@typeName(T), {}); | ||
try state.definitions.append(String.init(state.allocator)); | ||
const index = state.definitions.items.len - 1; | ||
|
||
try state.definitions.items[index].appendSlice("---@alias "); | ||
try state.definitions.items[index].appendSlice(name(T)); | ||
try state.definitions.items[index].appendSlice("\n"); | ||
|
||
inline for (@typeInfo(T).Enum.fields) |field| { | ||
try state.definitions.items[index].appendSlice("---|\' \""); | ||
try state.definitions.items[index].appendSlice(field.name); | ||
try state.definitions.items[index].appendSlice("\" \'\n"); | ||
} | ||
} | ||
} | ||
|
||
pub fn addClass( | ||
state: *DefineState, | ||
comptime T: type, | ||
) !void { | ||
if (state.database.contains(@typeName(T)) == false) { | ||
try state.database.put(@typeName(T), {}); | ||
try state.definitions.append(String.init(state.allocator)); | ||
const index = state.definitions.items.len - 1; | ||
|
||
try state.definitions.items[index].appendSlice("---@class (exact) "); | ||
try state.definitions.items[index].appendSlice(name(T)); | ||
try state.definitions.items[index].appendSlice("\n"); | ||
|
||
inline for (@typeInfo(T).Struct.fields) |field| { | ||
try state.definitions.items[index].appendSlice("---@field "); | ||
try state.definitions.items[index].appendSlice(field.name); | ||
|
||
if (field.default_value != null) { | ||
try state.definitions.items[index].appendSlice("?"); | ||
} | ||
try state.definitions.items[index].appendSlice(" "); | ||
try luaTypeName(state, index, field.type); | ||
try state.definitions.items[index].appendSlice("\n"); | ||
} | ||
} | ||
} | ||
|
||
fn luaTypeName( | ||
state: *DefineState, | ||
index: usize, | ||
comptime T: type, | ||
) !void { | ||
switch (@typeInfo(T)) { | ||
.Struct => { | ||
try state.definitions.items[index].appendSlice(name(T)); | ||
try addClass(state, T); | ||
}, | ||
.Pointer => |info| { | ||
if (info.child == u8 and info.size == .Slice) { | ||
try state.definitions.items[index].appendSlice("string"); | ||
} else switch (info.size) { | ||
.One => { | ||
try state.definitions.items[index].appendSlice("lightuserdata"); | ||
}, | ||
.C, .Many, .Slice => { | ||
try luaTypeName(state, index, info.child); | ||
try state.definitions.items[index].appendSlice("[]"); | ||
}, | ||
} | ||
}, | ||
.Array => |info| { | ||
try luaTypeName(state, index, info.child); | ||
try state.definitions.items[index].appendSlice("[]"); | ||
}, | ||
|
||
.Vector => |info| { | ||
try luaTypeName(state, index, info.child); | ||
try state.definitions.items[index].appendSlice("[]"); | ||
}, | ||
.Optional => |info| { | ||
try luaTypeName(state, index, info.child); | ||
try state.definitions.items[index].appendSlice(" | nil"); | ||
}, | ||
.Enum => { | ||
try state.definitions.items[index].appendSlice(name(T)); | ||
try addEnum(state, T); | ||
}, | ||
.Int => { | ||
try state.definitions.items[index].appendSlice("integer"); | ||
}, | ||
.Float => { | ||
try state.definitions.items[index].appendSlice("number"); | ||
}, | ||
.Bool => { | ||
try state.definitions.items[index].appendSlice("boolean"); | ||
}, | ||
else => { | ||
@compileLog(T); | ||
@compileError("Type not supported"); | ||
}, | ||
} | ||
} |
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.