Skip to content

Commit 2b5f463

Browse files
committed
mark functions with a magic type
1 parent 74406ab commit 2b5f463

4 files changed

Lines changed: 36 additions & 12 deletions

File tree

generated/secondlife.d.luau

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ export type PrimParamsSetterType = typeof(
230230
)
231231

232232

233-
declare function assert<T>(value: T?, message: string?): T
233+
declare function assert<T>(value: T?, message: string?): T -- magic type
234234
declare function dangerouslyexecuterequiredmodule(f: (...any) -> ...any): ...any
235235
declare function error(obj: any, level: number?): never
236236
declare function gcinfo(): number
@@ -247,10 +247,10 @@ declare function rawequal(a: any, b: any): boolean
247247
declare function rawget<K, V>(t: {[K]: V}, k: K): V?
248248
declare function rawlen<K, V>(t: {[K]: V} | string): number
249249
declare function rawset<K, V>(t: {[K]: V}, k: K, v: V): {[K]: V}
250-
declare function require(target: string): any
251-
declare function select(i: string | number, ...any): any
250+
declare function require(target: string): any -- magic type
251+
declare function select(i: string | number, ...any): any -- magic type
252252
declare setfenv: nil
253-
declare function setmetatable(t: { [any]: any }, mt: { [any]: any }?): ()
253+
declare function setmetatable(t: { [any]: any }, mt: { [any]: any }?): () -- magic type
254254
declare function tonumber(s: string, base: number?): number?
255255
declare function toquaternion(val: string? | quaternion): quaternion?
256256
declare function torotation(val: string? | quaternion): quaternion?
@@ -474,14 +474,14 @@ declare quaternion: ((x: number, y: number, z: number, s: number) -> quaternion)
474474
declare string: {
475475
byte: (s: string, i: number?, j: number?) -> ...number,
476476
char: (...number) -> string,
477-
find: (s: string, pattern: string, init: number?, plain: boolean?) -> (number?, number?, ...string),
478-
format: (formatstring: string, ...any) -> string,
479-
gmatch: (s: string, pattern: string) -> () -> ...string,
477+
find: (s: string, pattern: string, init: number?, plain: boolean?) -> (number?, number?, ...string), -- magic type
478+
format: (formatstring: string, ...any) -> string, -- magic type
479+
gmatch: (s: string, pattern: string) -> () -> ...string, -- magic type
480480
gsub: (s: string, pattern: string, repl: string | { [string]: string } | (...string) -> string, maxn: number?) -> (string, number),
481481
len: (s: string) -> number,
482482
lower: (s: string) -> string,
483-
match: (s: string, pattern: string, init: number?) -> ...string,
484-
pack: (fmt: string, ...any) -> string,
483+
match: (s: string, pattern: string, init: number?) -> ...string, -- magic type
484+
pack: (fmt: string, ...any) -> string, -- magic type
485485
packsize: (fmt: string) -> number,
486486
rep: (s: string, n: number) -> string,
487487
reverse: (s: string) -> string,
@@ -515,9 +515,9 @@ declare table: {
515515
find: <V>(t: {V}, v: V, i: number?) -> number?,
516516
clear: (t: {}) -> (),
517517
shrink: <V>(t: {V}, shrink_sparse: boolean?) -> {V},
518-
freeze: <table>(t: table) -> table,
518+
freeze: <table>(t: table) -> table, -- magic type
519519
isfrozen: (t: {}) -> boolean,
520-
clone: <table>(t: table) -> table,
520+
clone: <table>(t: table) -> table, -- magic type
521521
}
522522

523523

lsl_definitions/slua.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ class SLuaFunction(SLuaFunctionBase):
111111
must_use: bool = False
112112
"""Emit a warning if the return value is not used.
113113
See https://kampfkarren.github.io/selene/usage/std.html#must_use."""
114+
magic_type: bool = False
115+
"""The typechecker has custom logic for this function."""
114116
overloads: List[SLuaFunctionOverload] = dataclasses.field(default_factory=list)
115117

116118
@property
@@ -160,7 +162,10 @@ def write_luau_global_def(self, f: TextIO, indent: int = 0) -> None:
160162
f.write(f"function {self.name}")
161163
f.write(self.type_parameters_string)
162164
f.write(self.parameters_string)
163-
f.write(f": {self.return_type}\n")
165+
f.write(f": {self.return_type}")
166+
if self.magic_type:
167+
f.write(" -- magic type")
168+
f.write("\n")
164169

165170
def write_luau_table_def(self, f: TextIO, indent: int = 0, suffix=",") -> None:
166171
"""For declaring functions within a table/module"""
@@ -176,6 +181,8 @@ def write_luau_table_def(self, f: TextIO, indent: int = 0, suffix=",") -> None:
176181
f.write(overload.type_def_string)
177182
f.write(")")
178183
f.write(suffix)
184+
if self.magic_type:
185+
f.write(" -- magic type")
179186
f.write("\n")
180187

181188

@@ -762,6 +769,7 @@ def _validate_function(
762769
local_only=data.get("local-only", False),
763770
slua_removed=data.get("slua-removed", False),
764771
must_use=data.get("must-use", False),
772+
magic_type=data.get("magic-type", False),
765773
)
766774
self._validate_identifier(func.name)
767775
self._validate_scope(func.name, scope)

slua_definitions.schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,11 @@
187187
"description": "See https://github.com/secondlife/slua/blob/main/Common/include/Luau/Bytecode.h#L542",
188188
"type": "boolean"
189189
},
190+
"magic-type": {
191+
"const": true,
192+
"markdownDescription": "The typechecker has custom logic for this function. Look for attachMagicFunction in the source code.",
193+
"type": "boolean"
194+
},
190195
"comment": {
191196
"markdownDescription": "A brief description of this function.",
192197
"type": "string"

slua_definitions.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ functions:
359359
type: string?
360360
return-type: T
361361
fastcall: true
362+
magic-type: true
362363
- name: collectgarbage
363364
comment: Run the garbage collector
364365
parameters:
@@ -538,6 +539,7 @@ functions:
538539
comment: The name of the external module.
539540
type: string
540541
return-type: any
542+
magic-type: true
541543
- name: select
542544
comment: Returns a subset of arguments or the number of arguments.
543545
parameters:
@@ -549,6 +551,7 @@ functions:
549551
type: ...any
550552
return-type: any
551553
fastcall: true
554+
magic-type: true
552555
- name: setfenv
553556
comment: Set the scoped environment for the given function.
554557
parameters:
@@ -569,6 +572,7 @@ functions:
569572
type: "{ [any]: any }?"
570573
return-type: ()
571574
fastcall: true
575+
magic-type: true
572576
- name: tonumber
573577
comment: Converts the input string to a number in the specified base.
574578
parameters:
@@ -2048,6 +2052,7 @@ modules:
20482052
type: boolean?
20492053
return-type: (number?, number?, ...string)
20502054
must-use: true
2055+
magic-type: true
20512056
- name: format
20522057
comment: Formats input values into a string using printf-style format specifiers.
20532058
parameters:
@@ -2060,6 +2065,7 @@ modules:
20602065
optional: false
20612066
return-type: string
20622067
must-use: true
2068+
magic-type: true
20632069
- name: gmatch
20642070
comment: Returns an iterator function for pattern matches
20652071
parameters:
@@ -2071,6 +2077,7 @@ modules:
20712077
type: string
20722078
return-type: () -> ...string
20732079
must-use: true
2080+
magic-type: true
20742081
- name: gsub
20752082
comment: Performs pattern-based substitution in a string.
20762083
parameters:
@@ -2119,6 +2126,7 @@ modules:
21192126
type: number?
21202127
return-type: '...string'
21212128
must-use: true
2129+
magic-type: true
21222130
- name: pack
21232131
comment: Packs values into a binary string.
21242132
parameters:
@@ -2130,6 +2138,7 @@ modules:
21302138
type: ...any
21312139
return-type: string
21322140
must-use: true
2141+
magic-type: true
21332142
- name: packsize
21342143
comment: Returns the size of a packed string for the given format.
21352144
parameters:
@@ -2449,6 +2458,7 @@ modules:
24492458
type: table # should be: generic table. See #56
24502459
selene-type: table
24512460
return-type: table
2461+
magic-type: true
24522462
- name: isfrozen
24532463
comment: Returns true if a table is frozen.
24542464
parameters:
@@ -2467,6 +2477,7 @@ modules:
24672477
selene-type: table
24682478
return-type: table
24692479
must-use: true
2480+
magic-type: true
24702481
- name: utf8
24712482
comment: UTF-8 support library.
24722483
functions:

0 commit comments

Comments
 (0)