Skip to content

Commit 98bb655

Browse files
committed
Use more native types
1 parent 6b8c802 commit 98bb655

File tree

3 files changed

+52
-34
lines changed

3 files changed

+52
-34
lines changed

py/code.go

+44-26
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@ import (
88

99
// Code object
1010
type Code struct {
11-
Argcount int32 // #arguments, except *args
12-
Kwonlyargcount int32 // #keyword only arguments
13-
Nlocals int32 // #local variables
14-
Stacksize int32 // #entries needed for evaluation stack
15-
Flags int32 // CO_..., see below
16-
Code String // instruction opcodes
17-
Consts Tuple // list (constants used)
18-
Names Tuple // list of strings (names used)
19-
Varnames Tuple // tuple of strings (local variable names)
20-
Freevars Tuple // tuple of strings (free variable names)
21-
Cellvars Tuple // tuple of strings (cell variable names)
11+
Argcount int32 // #arguments, except *args
12+
Kwonlyargcount int32 // #keyword only arguments
13+
Nlocals int32 // #local variables
14+
Stacksize int32 // #entries needed for evaluation stack
15+
Flags int32 // CO_..., see below
16+
Code string // instruction opcodes
17+
Consts Tuple // list (constants used)
18+
Names []string // list of strings (names used)
19+
Varnames []string // tuple of strings (local variable names)
20+
Freevars []string // tuple of strings (free variable names)
21+
Cellvars []string // tuple of strings (cell variable names)
2222
// The rest doesn't count for hash or comparisons
2323
Cell2arg *byte // Maps cell vars which are arguments.
24-
Filename String // unicode (where it was loaded from)
25-
Name String // unicode (name, for reference)
24+
Filename string // unicode (where it was loaded from)
25+
Name string // unicode (name, for reference)
2626
Firstlineno int32 // first source line number
27-
Lnotab String // string (encoding addr<->lineno mapping) See Objects/lnotab_notes.txt for details.
27+
Lnotab string // string (encoding addr<->lineno mapping) See Objects/lnotab_notes.txt for details.
2828

2929
Weakreflist List // to support weakrefs to code objects
3030
}
@@ -103,14 +103,32 @@ func NewCode(argcount int32, kwonlyargcount int32,
103103

104104
// Type assert the objects
105105
consts := consts_.(Tuple)
106-
names := names_.(Tuple)
107-
varnames := varnames_.(Tuple)
108-
freevars := freevars_.(Tuple)
109-
cellvars := cellvars_.(Tuple)
110-
name := name_.(String)
111-
filename := filename_.(String)
112-
lnotab := lnotab_.(String)
113-
code := code_.(String)
106+
namesTuple := names_.(Tuple)
107+
varnamesTuple := varnames_.(Tuple)
108+
freevarsTuple := freevars_.(Tuple)
109+
cellvarsTuple := cellvars_.(Tuple)
110+
name := string(name_.(String))
111+
filename := string(filename_.(String))
112+
lnotab := string(lnotab_.(String))
113+
code := string(code_.(String))
114+
115+
// Convert Tuples to native []string for speed
116+
names := make([]string, len(namesTuple))
117+
for i := range namesTuple {
118+
names[i] = string(namesTuple[i].(String))
119+
}
120+
varnames := make([]string, len(varnamesTuple))
121+
for i := range varnamesTuple {
122+
varnames[i] = string(varnamesTuple[i].(String))
123+
}
124+
freevars := make([]string, len(freevarsTuple))
125+
for i := range freevarsTuple {
126+
freevars[i] = string(freevarsTuple[i].(String))
127+
}
128+
cellvars := make([]string, len(cellvarsTuple))
129+
for i := range cellvarsTuple {
130+
cellvars[i] = string(cellvarsTuple[i].(String))
131+
}
114132

115133
// Check argument types
116134
if argcount < 0 || kwonlyargcount < 0 || nlocals < 0 {
@@ -124,10 +142,10 @@ func NewCode(argcount int32, kwonlyargcount int32,
124142
// }
125143

126144
n_cellvars := len(cellvars)
127-
intern_strings(names)
128-
intern_strings(varnames)
129-
intern_strings(freevars)
130-
intern_strings(cellvars)
145+
intern_strings(namesTuple)
146+
intern_strings(varnamesTuple)
147+
intern_strings(freevarsTuple)
148+
intern_strings(cellvarsTuple)
131149
/* Intern selected string constants */
132150
for i := len(consts) - 1; i >= 0; i-- {
133151
if v, ok := consts[i].(String); ok {

py/function.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ type Function struct {
2828
KwDefaults StringDict // NULL or a dict
2929
Closure Tuple // NULL or a tuple of cell objects
3030
Doc Object // The __doc__ attribute, can be anything
31-
Name String // The __name__ attribute, a string object
31+
Name string // The __name__ attribute, a string object
3232
Dict StringDict // The __dict__ attribute, a dict or NULL
3333
Weakreflist List // List of weak references
3434
Module *Module // The __module__ attribute, can be anything
3535
Annotations StringDict // Annotations, a dict or NULL
36-
Qualname String // The qualified name
36+
Qualname string // The qualified name
3737
}
3838

3939
var FunctionType = NewType("function")
@@ -56,7 +56,7 @@ func (o *Function) Type() *Type {
5656
// attribute. qualname should be a unicode object or ""; if "", the
5757
// __qualname__ attribute is set to the same value as its __name__
5858
// attribute.
59-
func NewFunction(code *Code, globals StringDict, qualname String) *Function {
59+
func NewFunction(code *Code, globals StringDict, qualname string) *Function {
6060
var doc Object
6161
var module *Module
6262
if len(code.Consts) >= 1 {
@@ -99,7 +99,7 @@ func (f *Function) Call(self Object, args Tuple) Object {
9999
// Copy the args into the local variables
100100
locals := NewStringDict()
101101
for i := range args {
102-
locals[string(f.Code.Varnames[i].(String))] = args[i]
102+
locals[f.Code.Varnames[i]] = args[i]
103103
}
104104
fmt.Printf("locals = %v\n", locals)
105105
// FIXME return?

vm/eval.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ func do_WITH_CLEANUP(vm *Vm, arg int32) {
395395
// co_names of the code object. The compiler tries to use STORE_FAST
396396
// or STORE_GLOBAL if possible.
397397
func do_STORE_NAME(vm *Vm, namei int32) {
398-
vm.locals[string(vm.co.Names[namei].(py.String))] = vm.POP()
398+
vm.locals[vm.co.Names[namei]] = vm.POP()
399399
}
400400

401401
// Implements del name, where namei is the index into co_names
@@ -439,7 +439,7 @@ func do_LOAD_CONST(vm *Vm, consti int32) {
439439

440440
// Pushes the value associated with co_names[namei] onto the stack.
441441
func do_LOAD_NAME(vm *Vm, namei int32) {
442-
vm.PUSH(vm.co.Names[namei])
442+
vm.PUSH(py.String(vm.co.Names[namei]))
443443
}
444444

445445
// Creates a tuple consuming count items from the stack, and pushes
@@ -562,7 +562,7 @@ func do_STORE_MAP(vm *Vm, arg int32) {
562562

563563
// Pushes a reference to the local co_varnames[var_num] onto the stack.
564564
func do_LOAD_FAST(vm *Vm, var_num int32) {
565-
vm.PUSH(vm.locals[string(vm.co.Varnames[var_num].(py.String))])
565+
vm.PUSH(vm.locals[vm.co.Varnames[var_num]])
566566
}
567567

568568
// Stores TOS into the local co_varnames[var_num].
@@ -650,7 +650,7 @@ func do_MAKE_FUNCTION(vm *Vm, argc int32) {
650650
num_annotations := (argc >> 16) & 0x7fff
651651
qualname := vm.POP()
652652
code := vm.POP()
653-
function := py.NewFunction(code.(*py.Code), vm.globals, qualname.(py.String))
653+
function := py.NewFunction(code.(*py.Code), vm.globals, string(qualname.(py.String)))
654654

655655
// FIXME share code with MAKE_CLOSURE
656656
// if opcode == MAKE_CLOSURE {

0 commit comments

Comments
 (0)