Skip to content

Commit 909cd1f

Browse files
committed
all: stop using static variables in scopes
1 parent e62d132 commit 909cd1f

File tree

3 files changed

+152
-110
lines changed

3 files changed

+152
-110
lines changed

std/jule/sema/builtin.jule

Lines changed: 149 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -17,41 +17,46 @@ use "std/jule/types"
1717
// d: Data instance for evaluated expression of function.
1818
type builtinCaller = fn(mut &e: &eval, mut &fc: &ast::FuncCallExpr, mut &v: &Value): &Value
1919

20+
static mut hehe = map[str]&FuncIns{
21+
"print": &FuncIns{caller: builtinCallerPrint},
22+
}
23+
24+
static mut funcPrint: &FuncIns = nil
25+
static mut funcPrintln: &FuncIns = nil
26+
static mut funcNew: &FuncIns = nil
27+
static mut funcPanic: &FuncIns = nil
28+
static mut funcMake: &FuncIns = nil
29+
static mut funcAppend: &FuncIns = nil
30+
static mut funcCopy: &FuncIns = nil
31+
static mut funcLen: &FuncIns = nil
32+
static mut funcCap: &FuncIns = nil
33+
static mut funcDelete: &FuncIns = nil
34+
static mut funcClose: &FuncIns = nil
35+
2036
fn findBuiltinFunc(&ident: str): &FuncIns {
2137
match ident {
2238
| "print":
23-
static mut f = &FuncIns{caller: builtinCallerPrint}
24-
ret f
39+
ret funcPrint
2540
| "println":
26-
static mut f = &FuncIns{caller: builtinCallerPrintln}
27-
ret f
41+
ret funcPrintln
2842
| "new":
29-
static mut f = &FuncIns{caller: builtinCallerNew}
30-
ret f
43+
ret funcNew
3144
| "panic":
32-
static mut f = &FuncIns{caller: builtinCallerPanic}
33-
ret f
45+
ret funcPanic
3446
| "make":
35-
static mut f = &FuncIns{caller: builtinCallerMake}
36-
ret f
47+
ret funcMake
3748
| "append":
38-
static mut f = &FuncIns{caller: builtinCallerAppend}
39-
ret f
49+
ret funcAppend
4050
| "copy":
41-
static mut f = &FuncIns{caller: builtinCallerCopy}
42-
ret f
51+
ret funcCopy
4352
| "len":
44-
static mut f = &FuncIns{caller: builtinCallerLen}
45-
ret f
53+
ret funcLen
4654
| "cap":
47-
static mut f = &FuncIns{caller: builtinCallerCap}
48-
ret f
55+
ret funcCap
4956
| "delete":
50-
static mut f = &FuncIns{caller: builtinCallerDelete}
51-
ret f
57+
ret funcDelete
5258
| "close":
53-
static mut f = &FuncIns{caller: builtinCallerClose}
54-
ret f
59+
ret funcClose
5560
|:
5661
ret nil
5762
}
@@ -112,99 +117,102 @@ static mut varNil = &Var{
112117
},
113118
}
114119

120+
static mut varTrue = &Var{
121+
Public: true,
122+
Mutable: false,
123+
TypeSym: findBuiltinTypeAlias("bool").TypeSym,
124+
Constant: true,
125+
ValueSym: &ValueSym{
126+
Value: &Value{
127+
Constant: constant::Const.NewBool(true),
128+
untyped: true,
129+
},
130+
},
131+
}
132+
133+
static mut varFalse = &Var{
134+
Public: true,
135+
Mutable: false,
136+
TypeSym: findBuiltinTypeAlias("bool").TypeSym,
137+
Constant: true,
138+
ValueSym: &ValueSym{
139+
Value: &Value{
140+
Constant: constant::Const.NewBool(false),
141+
untyped: true,
142+
},
143+
},
144+
}
145+
115146
fn findBuiltinVar(&ident: str): &Var {
116147
match ident {
117148
| "nil":
118149
ret varNil
119150
| "true":
120-
static mut v = &Var{
121-
Public: true,
122-
Mutable: false,
123-
TypeSym: findBuiltinTypeAlias("bool").TypeSym,
124-
Constant: true,
125-
ValueSym: &ValueSym{
126-
Value: &Value{
127-
Constant: constant::Const.NewBool(true),
128-
untyped: true,
129-
},
130-
},
131-
}
132-
ret v
151+
ret varTrue
133152
| "false":
134-
static mut v = &Var{
135-
Public: true,
136-
Mutable: false,
137-
TypeSym: findBuiltinTypeAlias("bool").TypeSym,
138-
Constant: true,
139-
ValueSym: &ValueSym{
140-
Value: &Value{
141-
Constant: constant::Const.NewBool(false),
142-
untyped: true,
143-
},
144-
},
145-
}
146-
ret v
153+
ret varFalse
147154
|:
148155
ret nil
149156
}
150157
}
151158

159+
static mut typAny = primTypeAlias("any", primAny, false)
160+
static mut typStr = primTypeAlias("str", primStr, false)
161+
static mut typBool = primTypeAlias("bool", primBool, false)
162+
static mut typUintptr = primTypeAlias("uintptr", primUintptr, false)
163+
static mut typUint = primTypeAlias("uint", primUint, false)
164+
static mut typInt = primTypeAlias("int", primInt, false)
165+
static mut typI8 = primTypeAlias("i8", primI8, false)
166+
static mut typI16 = primTypeAlias("i16", primI16, false)
167+
static mut typI32 = primTypeAlias("i32", primI32, false)
168+
static mut typI64 = primTypeAlias("i64", primI64, false)
169+
static mut typU8 = primTypeAlias("u8", primU8, false)
170+
static mut typU16 = primTypeAlias("u16", primU16, false)
171+
static mut typU32 = primTypeAlias("u32", primU32, false)
172+
static mut typU64 = primTypeAlias("u64", primU64, false)
173+
static mut typF32 = primTypeAlias("f32", primF32, false)
174+
static mut typF64 = primTypeAlias("f64", primF64, false)
175+
static mut typByte = primTypeAlias("byte", primU8, true)
176+
static mut typRune = primTypeAlias("rune", primI32, false)
177+
152178
fn findBuiltinTypeAlias(ident: str): &TypeAlias {
153179
match ident {
154180
| "any":
155-
static mut t = primTypeAlias("any", primAny, false)
156-
ret t
181+
ret typAny
157182
| "str":
158-
static mut t = primTypeAlias("str", primStr, false)
159-
ret t
183+
ret typStr
160184
| "bool":
161-
static mut t = primTypeAlias("bool", primBool, false)
162-
ret t
185+
ret typBool
163186
| "uintptr":
164-
static mut t = primTypeAlias("uintptr", primUintptr, false)
165-
ret t
187+
ret typUintptr
166188
| "uint":
167-
static mut t = primTypeAlias("uint", primUint, false)
168-
ret t
189+
ret typUint
169190
| "int":
170-
static mut t = primTypeAlias("int", primInt, false)
171-
ret t
191+
ret typInt
172192
| "i8":
173-
static mut t = primTypeAlias("i8", primI8, false)
174-
ret t
193+
ret typI8
175194
| "i16":
176-
static mut t = primTypeAlias("i16", primI16, false)
177-
ret t
195+
ret typI16
178196
| "i32":
179-
static mut t = primTypeAlias("i32", primI32, false)
180-
ret t
197+
ret typI32
181198
| "i64":
182-
static mut t = primTypeAlias("i64", primI64, false)
183-
ret t
199+
ret typI64
184200
| "u8":
185-
static mut t = primTypeAlias("u8", primU8, false)
186-
ret t
201+
ret typU8
187202
| "u16":
188-
static mut t = primTypeAlias("u16", primU16, false)
189-
ret t
203+
ret typU16
190204
| "u32":
191-
static mut t = primTypeAlias("u32", primU32, false)
192-
ret t
205+
ret typU32
193206
| "u64":
194-
static mut t = primTypeAlias("u64", primU64, false)
195-
ret t
207+
ret typU64
196208
| "f32":
197-
static mut t = primTypeAlias("f32", primF32, false)
198-
ret t
209+
ret typF32
199210
| "f64":
200-
static mut t = primTypeAlias("f64", primF64, false)
201-
ret t
211+
ret typF64
202212
| "byte":
203-
static mut t = primTypeAlias("byte", primU8, true)
204-
ret t
213+
ret typByte
205214
| "rune":
206-
static mut t = primTypeAlias("rune", primI32, false)
207-
ret t
215+
ret typRune
208216
|:
209217
ret nil
210218
}
@@ -226,56 +234,54 @@ fn findBuiltinDef(&ident: str): any {
226234
ret nil
227235
}
228236

237+
static mut funcSizeOf: &FuncIns = nil
238+
static mut funcAlignOf: &FuncIns = nil
239+
229240
fn findBuiltinDefStdMem(&ident: str): any {
230241
match ident {
231242
| "SizeOf":
232-
static mut f = &FuncIns{caller: builtinCallerStdMemSizeOf}
233-
ret f
243+
ret funcSizeOf
234244
| "AlignOf":
235-
static mut f = &FuncIns{caller: builtinCallerStdMemAlignOf}
236-
ret f
245+
ret funcAlignOf
237246
|:
238247
ret nil
239248
}
240249
}
241250

251+
static mut funcTypeOf: &FuncIns = nil
252+
static mut funcValueOf: &FuncIns = nil
253+
static mut funcLine: &FuncIns = nil
254+
static mut funcFile: &FuncIns = nil
255+
static mut funcFiles: &FuncIns = nil
256+
static mut funcTypeAlias: &FuncIns = nil
257+
242258
fn findBuiltinDefStdComptime(&ident: str): any {
243259
match ident {
244260
| "TypeOf":
245-
static mut f = &FuncIns{caller: builtinCallerStdComptimeTypeOf}
246-
ret f
261+
ret funcTypeOf
247262
| "ValueOf":
248-
static mut f = &FuncIns{caller: builtinCallerStdComptimeValueOf}
249-
ret f
263+
ret funcValueOf
250264
| "Line":
251-
static mut f = &FuncIns{caller: builtinCallerStdComptimeLine}
252-
ret f
265+
ret funcLine
253266
| "File":
254-
static mut f = &FuncIns{caller: builtinCallerStdComptimeFile}
255-
ret f
267+
ret funcFile
256268
| "Files":
257-
static mut f = &FuncIns{caller: builtinCallerStdComptimeFiles}
258-
ret f
269+
ret funcFiles
259270
| "TypeAlias":
260-
static mut f = &FuncIns{caller: builtinCallerStdComptimeTypeAlias}
261-
ret f
271+
ret funcTypeAlias
262272
|:
263273
ret nil
264274
}
265275
}
266276

277+
static mut funcEmit: &FuncIns = nil
278+
267279
fn findBuiltinDefStdJuleIntegrated(&ident: str): any {
268280
match ident {
269281
| "Emit":
270-
static mut f = &FuncIns{
271-
Decl: &Func{
272-
Generics: make([]&ast::Generic, 1),
273-
},
274-
caller: builtinCallerStdJuleIntegratedEmit,
275-
}
276282
// Reset generics for every reference because of common instance.
277-
f.Generics = nil
278-
ret f
283+
funcEmit.Generics = nil
284+
ret funcEmit
279285
|:
280286
ret nil
281287
}
@@ -1272,4 +1278,39 @@ fn buildIota(i: i64): &Var {
12721278
},
12731279
},
12741280
}
1281+
}
1282+
1283+
fn init() {
1284+
// Initialize built-in functions.
1285+
funcPrint = &FuncIns{caller: builtinCallerPrint}
1286+
funcPrintln = &FuncIns{caller: builtinCallerPrintln}
1287+
funcNew = &FuncIns{caller: builtinCallerNew}
1288+
funcPanic = &FuncIns{caller: builtinCallerPanic}
1289+
funcMake = &FuncIns{caller: builtinCallerMake}
1290+
funcAppend = &FuncIns{caller: builtinCallerAppend}
1291+
funcCopy = &FuncIns{caller: builtinCallerCopy}
1292+
funcLen = &FuncIns{caller: builtinCallerLen}
1293+
funcCap = &FuncIns{caller: builtinCallerCap}
1294+
funcDelete = &FuncIns{caller: builtinCallerDelete}
1295+
funcClose = &FuncIns{caller: builtinCallerClose}
1296+
1297+
// Initialize built-in function of the "std/mem" package.
1298+
funcSizeOf = &FuncIns{caller: builtinCallerStdMemSizeOf}
1299+
funcAlignOf = &FuncIns{caller: builtinCallerStdMemAlignOf}
1300+
1301+
// Initialize built-in function of the "std/comptime" package.
1302+
funcTypeOf = &FuncIns{caller: builtinCallerStdComptimeTypeOf}
1303+
funcValueOf = &FuncIns{caller: builtinCallerStdComptimeValueOf}
1304+
funcLine = &FuncIns{caller: builtinCallerStdComptimeLine}
1305+
funcFile = &FuncIns{caller: builtinCallerStdComptimeFile}
1306+
funcFiles = &FuncIns{caller: builtinCallerStdComptimeFiles}
1307+
funcTypeAlias = &FuncIns{caller: builtinCallerStdComptimeTypeAlias}
1308+
1309+
// Initialize built-in function of the "std/jule/integrated" package.
1310+
funcEmit = &FuncIns{
1311+
Decl: &Func{
1312+
Generics: make([]&ast::Generic, 1),
1313+
},
1314+
caller: builtinCallerStdJuleIntegratedEmit,
1315+
}
12751316
}

std/math/cmplx/cmplx.jule

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ impl Cmplx {
115115
mut a, mut b := self.Real(), self.Imag()
116116
mut c, mut d := m.Real(), m.Imag()
117117

118-
static inf = math::F64frombits(0x7FF0000000000000)
118+
inf := math::F64frombits(0x7FF0000000000000)
119119

120120
match {
121121
| IsZero(m) && (!math::IsNaN(a) || !math::IsNaN(b)):

std/time/zoneinfo.jule

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,15 @@ struct Location {
3838
cacheZone: &zone
3939
}
4040

41+
static localOnce = sync::Once.New()
42+
4143
impl Location {
4244
// Returns a descriptive name for the time zone information.
4345
fn Str(self): str {
4446
ret unsafe { (&Location)(&self).get().name }
4547
}
4648

4749
fn get(mut &self): &Location {
48-
static localOnce = sync::Once.New()
4950
if self == nil {
5051
ret unsafe { *(&UTC) }
5152
}

0 commit comments

Comments
 (0)