Skip to content

Commit a929c53

Browse files
committed
Fixed .d.ts typings generation
Fixed some examples Miscellaneous fixes
1 parent af49842 commit a929c53

30 files changed

Lines changed: 63202 additions & 3388 deletions

CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,12 @@ message("=== Configure QuickJS ===")
5959
# add quickjs
6060
set(quickjs_version 2024-09-28)
6161
set(quickjs_sources_root ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/quickjs)
62+
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/overrides/quickjs/quickjs.h ${quickjs_sources_root}/quickjs.h @ONLY)
63+
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/overrides/quickjs/quickjs.c ${quickjs_sources_root}/quickjs.c @ONLY)
6264
set(quickjs_sources
6365
${quickjs_sources_root}/quickjs.h
64-
${quickjs_sources_root}/quickjs-atom.h
6566
${quickjs_sources_root}/quickjs.c
67+
${quickjs_sources_root}/quickjs-atom.h
6668
${quickjs_sources_root}/libregexp.c
6769
${quickjs_sources_root}/libunicode.c
6870
${quickjs_sources_root}/xsum.c

bindings/src/cgen.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ export function getVariableParts(input,variables){
248248
if(input[pos]==')')pos++;
249249
}
250250
capture=[];
251-
pos=simpleregex(input,["nr*","*/+-([{.'\""],pos,capture);
251+
pos=simpleregex(input,["nr*","*/+-&([{.'\""],pos,capture);
252252
let str=capture[0].trim();
253253
if(str!=''){
254254
if(isNaN(str)){
@@ -304,6 +304,9 @@ export function getVariableParts(input,variables){
304304
}
305305
pos++;
306306
}
307+
}else if(input[pos]=='&'){
308+
parts.push({type:'ref'});
309+
pos++;
307310
}else if(input[pos]=='.'){
308311
pos++;
309312
}else if('*/+-'.includes(input[pos])){
@@ -592,6 +595,13 @@ export function resolveVariable(variableParts,targetType,variableList){
592595
}
593596
init();
594597
}break;
598+
case "ref":{
599+
resolved='&'+resolved;
600+
if(fields.length>0){
601+
fields=[];
602+
}
603+
type+=' *';
604+
}break;
595605
}
596606
}
597607
//colapse math

bindings/src/index.js

Lines changed: 58 additions & 58 deletions
Large diffs are not rendered by default.

bindings/src/quickjs.js

Lines changed: 62 additions & 74 deletions
Large diffs are not rendered by default.

bindings/src/raylib-header.js

Lines changed: 93 additions & 85 deletions
Large diffs are not rendered by default.

bindings/src/source_parser.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ export class source_parser {
476476
thiz.defineName(capture[7],'callbacks',{
477477
returnType: thiz.normalizeType(capture[1]+capture[2]+capture[3]+capture[4]),
478478
name: capture[7],
479-
params: thiz.parseFunctionArgs(capture[12]+capture[13]),
479+
args: thiz.parseFunctionArgs(capture[12]+capture[13]),
480480
type,
481481
binding:{}
482482
});
@@ -649,7 +649,7 @@ export class source_parser {
649649
this.defineName(capture[0],'functions',{
650650
returnType: this.normalizeType(capture[2]+capture[1]),
651651
name: capture[0],
652-
params: this.parseFunctionArgs(capture[3]+capture[4]),
652+
args: this.parseFunctionArgs(capture[3]+capture[4]),
653653
props,
654654
binding:{}
655655
});

bindings/src/typescript.js

Lines changed: 87 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,53 +8,34 @@ export class TypeScriptDeclaration {
88
this.module = this.root.tsDeclareModule(modulename);
99
this.module.name = modulename;
1010
}
11-
12-
this.includeList={};//{key=>{value:true}}
13-
this.includes = this.module.child();
14-
this.includes.tags.module=this.module.tags.module;
15-
this.structs = this.module.child();
16-
this.structs.tags.module=this.module.tags.module;
17-
this.functions = this.module.child();
18-
this.functions.tags.module=this.module.tags.module;
19-
this.constants = this.module.child();
20-
this.constants.tags.module=this.module.tags.module;
2111
this.defined={};
12+
this.functions=[];
13+
this.structs=[];
14+
this.callbacks=[];
15+
this.aliases=[];
16+
this.constants=[];
2217
}
23-
addFunction(name, api, lookups) {
24-
const options = api.binding || {};
25-
const para = (api.params || []).filter(x => !x.binding.ignore).map(x => ({ name: (x.type=='...'||x.binding.jsType=='...'?'...':'')+x.name, type: x.binding.jsType ?? this.toJsType(x.type,lookups) }));
26-
const returnType = options.jsReturns ?? this.toJsType(api.returnType,lookups);
27-
this.functions.tsDeclareFunction(name, para, returnType, api.description);
18+
addConstant(name, type, description){
2819
this.defined[name]=true;
20+
this.constants.push([name, type, description]);
21+
}
22+
addFunction(api) {
23+
this.defined[api.name]=true;
24+
this.functions.push(api);
2925
}
3026
addStruct(api) {
31-
const options = api.binding || {};
32-
var fields = api.fields.filter(x => !!(options.properties || {})[x.name]).map(x => ({ name: x.name, description: x.description, type: this.toJsType(x.type) }));
33-
this.structs.tsDeclareInterface(api.name, fields);
34-
this.structs.tsDeclareType(api.name, !!(options.createConstructor), fields);
3527
this.defined[api.name]=true;
28+
if(api.fields==undefined)debugger;
29+
this.structs.push(api);
3630
}
37-
addCallback(name, parameters, lookups){
31+
addCallback(name, parameters){
3832
if(this.defined[name])return;
39-
let ret = parameters.returnType;
40-
if(ret==undefined) {
41-
ret=='void';
42-
}else{
43-
ret = this.toJsType(ret,lookups);
44-
}
45-
let inlinefn;
46-
if(parameters.threaded){
47-
//when inside of a worker, we pass the indentyfier that will be caught by onmessage
48-
inlinefn = 'string | number';
49-
}else{
50-
inlinefn = `(${parameters.map(a=>`${a.name}:${this.toJsType(a.type.replaceAll('&', '*'),lookups)}`).join(',')})=>${ret}`;
51-
}
52-
this.structs.tsAliasType(name, inlinefn);
5333
this.defined[name]=true;
34+
this.callbacks.push({name, parameters});
5435
}
55-
addAlias(name, aliasof, lookups){
56-
aliasof = this.toJsType(aliasof,lookups);
57-
this.structs.tsAliasType(name, aliasof);
36+
addAlias(name, aliasof){
37+
this.defined[name]=true;
38+
this.aliases.push({name, aliasof});
5839
}
5940
toJsType(type,lookups) {
6041
if(type.startsWith('const ')){
@@ -64,10 +45,18 @@ export class TypeScriptDeclaration {
6445
let variable=lookups.variables[type];
6546
if(variable){
6647
//custom type
67-
let sourceModule=lookups.includegen.tokenList.find(a=>a.type=='include'&&a.text.vars.find(b=>b.name==type)!=undefined);
68-
if(sourceModule.text.name != this.module.name){
69-
if(this.includeList[sourceModule]==undefined)this.includeList[sourceModule]=[];
70-
this.includeList[sourceModule][type]=true;
48+
let sourceModule=lookups.includegen.tokenList.find(a=>a.type=='include'&&a.text.vars.find(b=>b.name==type)!=undefined).text.name;
49+
let moduletypingName;
50+
for(let key in lookups.modules){
51+
const module=lookups.modules[key];
52+
if(module.gen.c_source==sourceModule){
53+
moduletypingName='rayjs:'+module.gen.name;
54+
break;
55+
}
56+
}
57+
if(moduletypingName != this.module.name){
58+
if(this.includeList[moduletypingName]==undefined)this.includeList[moduletypingName]={};
59+
this.includeList[moduletypingName][type]=true;
7160
}
7261
}
7362
}
@@ -145,11 +134,62 @@ export class TypeScriptDeclaration {
145134
return type.replace(" *", "").replace("const ", "");
146135
}
147136
}
148-
writeTo(filename) {
137+
writeTo(filename, lookups) {
149138
//reset includes
139+
this.includeList={};//{key=>{value:true}}
140+
this.includesGen = this.module.child();
141+
this.includesGen.tags.module=this.module.tags.module;
142+
this.structsGen = this.module.child();
143+
this.structsGen.tags.module=this.module.tags.module;
144+
this.functionsGen = this.module.child();
145+
this.functionsGen.tags.module=this.module.tags.module;
146+
this.constantsGen = this.module.child();
147+
this.constantsGen.tags.module=this.module.tags.module;
148+
149+
//Structs
150+
for(let struct of this.structs){
151+
const options = struct.binding || {};
152+
var fields = struct.fields.filter(x => x.binding.get).map(x => ({ name: x.name, description: x.description, type: this.toJsType(x.type) }));
153+
this.structsGen.tsDeclareInterface(struct.name, fields);
154+
this.structsGen.tsDeclareType(struct.name, !!(options.createConstructor), fields);
155+
}
156+
//Callbacks
157+
for(let callback of this.callbacks){
158+
let ret = callback.parameters.returnType;
159+
if(ret==undefined) {
160+
ret=='void';
161+
}else{
162+
ret = this.toJsType(ret,lookups);
163+
}
164+
let inlinefn;
165+
if(callback.parameters.threaded){
166+
//when inside of a worker, we pass the indentyfier that will be caught by onmessage
167+
inlinefn = 'string | number';
168+
}else{
169+
inlinefn = `(${callback.parameters.map(a=>`${a.name}:${this.toJsType(a.type.replaceAll('&', '*'),lookups)}`).join(',')})=>${ret}`;
170+
}
171+
this.structsGen.tsAliasType(callback.name, inlinefn);
172+
}
173+
//Aliases
174+
for(let alias of this.aliases){
175+
const aliasof = this.toJsType(alias.aliasof,lookups);
176+
this.structsGen.tsAliasType(alias.name, aliasof);
177+
}
178+
//Constants
179+
for(let constant of this.constants){
180+
this.constantsGen.tsDeclareConstant(...constant);
181+
}
182+
//Functions
183+
for(let fn of this.functions){
184+
const options = fn.binding || {};
185+
const arg = fn.args.filter(x => !x.binding.ignore).map(x => ({ name: (x.type=='...'?'...':'')+x.name, type: x.binding.jsType ?? this.toJsType(x.type,lookups) }));
186+
const returnType = options.jsReturns ?? this.toJsType(fn.returnType,lookups);
187+
this.functionsGen.tsDeclareFunction(fn.name, arg, returnType, fn.description);
188+
}
189+
150190
for(let key in this.includeList){
151191
let toload = this.includeList[key];
152-
this.includes.tsImportTypes(key,Object.keys(toload));
192+
this.includesGen.tsImportTypes(key,Object.keys(toload));
153193
}
154194
//generate code
155195
writeFileSync(filename, this.root.toString());
@@ -200,9 +240,9 @@ class TypescriptGenerator {
200240
let declare =this.tags.module?'':'declare ';
201241
this.line(`${declare}var ${name}: {\n`);
202242
this.indent();
203-
this.line("prototype: " + name);
243+
this.line(`prototype: ${name}\n`);
204244
if (hasConstructor)
205-
this.line(`new(${parameters.map(x => x.name + "?: " + x.type).join(', ')}): ${name}`);
245+
this.line(`new(${parameters.map(x => x.name + "?: " + x.type).join(', ')}): ${name}\n`);
206246
this.unindent();
207247
this.line("}\n");
208248
}
@@ -223,7 +263,7 @@ class TypescriptGenerator {
223263
this.line("}\n");
224264
}
225265
tsImportTypes(module,types){
226-
this.line(`import type {${types.join(',')}} from '${module}'`);
266+
this.line(`import type {${types.join(',')}} from '${module}';\n`);
227267
}
228268
tsDeclareModule(name){
229269
this.line(`declare module "${name}" {\n`);
@@ -235,7 +275,7 @@ class TypescriptGenerator {
235275
return child;
236276
}
237277
tsAliasType(name, aliasof){
238-
this.line(`type ${name} = ${aliasof}`);
278+
this.line(`type ${name} = ${aliasof};\n`);
239279
}
240280
tsDocComment(comment) {
241281
this.line(`/** ${comment} */\n`);

bindings/typings/lib.js_raygui.d.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
declare module "rayjs:raygui" {
2-
import type {Font,Color,Rectangle,Vector2,Vector3} from '[object Object]'interface GuiStyleProp {
2+
import type {Font,Color,Rectangle,Vector2,Vector3} from 'rayjs:raylib';
3+
interface GuiStyleProp {
4+
controlId: number,
5+
propertyId: number,
6+
propertyValue: number,
37
}
48
var GuiStyleProp: {
5-
prototype: GuiStylePropnew(): GuiStyleProp}
9+
prototype: GuiStyleProp
10+
new(controlId?: number, propertyId?: number, propertyValue?: number): GuiStyleProp
11+
}
612
/** undefined */
713
function GuiEnable(): void/** undefined */
814
function GuiDisable(): void/** undefined */

0 commit comments

Comments
 (0)