| 1 |
package starlark |
| 2 |
|
| 3 |
// This file defines the bytecode interpreter. |
| 4 |
|
| 5 |
import ( |
| 6 |
"fmt" |
| 7 |
"os" |
| 8 |
"slices" |
| 9 |
|
| 10 |
"go.starlark.net/internal/compile" |
| 11 |
"go.starlark.net/internal/spell" |
| 12 |
"go.starlark.net/syntax" |
| 13 |
) |
| 14 |
|
| 15 |
const vmdebug = false // TODO(adonovan): use a bitfield of specific kinds of error. |
| 16 |
|
| 17 |
// TODO(adonovan): |
| 18 |
// - optimize position table. |
| 19 |
// - opt: record MaxIterStack during compilation and preallocate the stack. |
| 20 |
|
| 21 |
func (fn *Function) CallInternal(thread *Thread, args Tuple, kwargs []Tuple) (Value, error) { |
| 22 |
// Postcondition: args is not mutated. This is stricter than required by Callable, |
| 23 |
// but allows CALL to avoid a copy. |
| 24 |
|
| 25 |
f := fn.funcode |
| 26 |
if f.Prog.Recursion { |
| 27 |
// prevent stack overflow |
| 28 |
// |
| 29 |
// Each CallInternal recursion (via Call) uses ~1.4KB, |
| 30 |
// but the stack limit is on the order of 1GB, so a |
| 31 |
// maximum of about 700K recursive calls is possible. |
| 32 |
// Limit it to much less here. |
| 33 |
if len(thread.stack) > 100_000 { |
| 34 |
return nil, fmt.Errorf("Starlark stack overflow") |
| 35 |
} |
| 36 |
} else { |
| 37 |
// detect recursion |
| 38 |
for _, fr := range thread.stack[:len(thread.stack)-1] { |
| 39 |
// We look for the same function code, |
| 40 |
// not function value, otherwise the user could |
| 41 |
// defeat the check by writing the Y combinator. |
| 42 |
if frfn, ok := fr.Callable().(*Function); ok && frfn.funcode == f { |
| 43 |
return nil, fmt.Errorf("function %s called recursively", fn.Name()) |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
fr := thread.frameAt(0) |
| 49 |
|
| 50 |
// Allocate space for stack and locals. |
| 51 |
// Logically these do not escape from this frame |
| 52 |
// (See https://github.com/golang/go/issues/20533.) |
| 53 |
// |
| 54 |
// This heap allocation looks expensive, but I was unable to get |
| 55 |
// more than 1% real time improvement in a large alloc-heavy |
| 56 |
// benchmark (in which this alloc was 8% of alloc-bytes) |
| 57 |
// by allocating space for 8 Values in each frame, or |
| 58 |
// by allocating stack by slicing an array held by the Thread |
| 59 |
// that is expanded in chunks of min(k, nspace), for k=256 or 1024. |
| 60 |
nlocals := len(f.Locals) |
| 61 |
nspace := nlocals + f.MaxStack |
| 62 |
space := make([]Value, nspace) |
| 63 |
locals := space[:nlocals:nlocals] // local variables, starting with parameters |
| 64 |
stack := space[nlocals:] // operand stack |
| 65 |
|
| 66 |
// Digest arguments and set parameters. |
| 67 |
err := setArgs(locals, fn, args, kwargs) |
| 68 |
if err != nil { |
| 69 |
return nil, thread.evalError(err) |
| 70 |
} |
| 71 |
|
| 72 |
fr.locals = locals |
| 73 |
|
| 74 |
if vmdebug { |
| 75 |
fmt.Printf("Entering %s @ %s\n", f.Name, f.Position(0)) |
| 76 |
fmt.Printf("%d stack, %d locals\n", len(stack), len(locals)) |
| 77 |
defer fmt.Println("Leaving ", f.Name) |
| 78 |
} |
| 79 |
|
| 80 |
// Spill indicated locals to cells. |
| 81 |
// Each cell is a separate alloc to avoid spurious liveness. |
| 82 |
for _, index := range f.Cells { |
| 83 |
locals[index] = &cell{locals[index]} |
| 84 |
} |
| 85 |
|
| 86 |
// TODO(adonovan): add static check that beneath this point |
| 87 |
// - there is exactly one return statement |
| 88 |
// - there is no redefinition of 'err'. |
| 89 |
|
| 90 |
var iterstack []Iterator // stack of active iterators |
| 91 |
|
| 92 |
// Use defer so that application panics can pass through |
| 93 |
// interpreter without leaving thread in a bad state. |
| 94 |
defer func() { |
| 95 |
// ITERPOP the rest of the iterator stack. |
| 96 |
for _, iter := range iterstack { |
| 97 |
iter.Done() |
| 98 |
} |
| 99 |
|
| 100 |
fr.locals = nil |
| 101 |
}() |
| 102 |
|
| 103 |
sp := 0 |
| 104 |
var pc uint32 |
| 105 |
var result Value |
| 106 |
code := f.Code |
| 107 |
loop: |
| 108 |
for { |
| 109 |
thread.Steps++ |
| 110 |
if thread.Steps >= thread.maxSteps { |
| 111 |
if thread.OnMaxSteps != nil { |
| 112 |
thread.OnMaxSteps(thread) |
| 113 |
} else { |
| 114 |
thread.Cancel("too many steps") |
| 115 |
} |
| 116 |
} |
| 117 |
if reason := thread.cancelReason.Load(); reason != nil { |
| 118 |
err = fmt.Errorf("Starlark computation cancelled: %s", *reason) |
| 119 |
break loop |
| 120 |
} |
| 121 |
|
| 122 |
fr.pc = pc |
| 123 |
|
| 124 |
op := compile.Opcode(code[pc]) |
| 125 |
pc++ |
| 126 |
var arg uint32 |
| 127 |
if op >= compile.OpcodeArgMin { |
| 128 |
// TODO(adonovan): opt: profile this. |
| 129 |
// Perhaps compiling big endian would be less work to decode? |
| 130 |
for s := uint(0); ; s += 7 { |
| 131 |
b := code[pc] |
| 132 |
pc++ |
| 133 |
arg |= uint32(b&0x7f) << s |
| 134 |
if b < 0x80 { |
| 135 |
break |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
if vmdebug { |
| 140 |
fmt.Fprintln(os.Stderr, stack[:sp]) // very verbose! |
| 141 |
compile.PrintOp(f, fr.pc, op, arg) |
| 142 |
} |
| 143 |
|
| 144 |
switch op { |
| 145 |
case compile.NOP: |
| 146 |
// nop |
| 147 |
|
| 148 |
case compile.DUP: |
| 149 |
stack[sp] = stack[sp-1] |
| 150 |
sp++ |
| 151 |
|
| 152 |
case compile.DUP2: |
| 153 |
stack[sp] = stack[sp-2] |
| 154 |
stack[sp+1] = stack[sp-1] |
| 155 |
sp += 2 |
| 156 |
|
| 157 |
case compile.POP: |
| 158 |
sp-- |
| 159 |
|
| 160 |
case compile.EXCH: |
| 161 |
stack[sp-2], stack[sp-1] = stack[sp-1], stack[sp-2] |
| 162 |
|
| 163 |
case compile.EQL, compile.NEQ, compile.GT, compile.LT, compile.LE, compile.GE: |
| 164 |
op := syntax.Token(op-compile.EQL) + syntax.EQL |
| 165 |
y := stack[sp-1] |
| 166 |
x := stack[sp-2] |
| 167 |
sp -= 2 |
| 168 |
ok, err2 := Compare(op, x, y) |
| 169 |
if err2 != nil { |
| 170 |
err = err2 |
| 171 |
break loop |
| 172 |
} |
| 173 |
stack[sp] = Bool(ok) |
| 174 |
sp++ |
| 175 |
|
| 176 |
case compile.PLUS, |
| 177 |
compile.MINUS, |
| 178 |
compile.STAR, |
| 179 |
compile.SLASH, |
| 180 |
compile.SLASHSLASH, |
| 181 |
compile.PERCENT, |
| 182 |
compile.AMP, |
| 183 |
compile.PIPE, |
| 184 |
compile.CIRCUMFLEX, |
| 185 |
compile.LTLT, |
| 186 |
compile.GTGT, |
| 187 |
compile.IN: |
| 188 |
binop := syntax.Token(op-compile.PLUS) + syntax.PLUS |
| 189 |
if op == compile.IN { |
| 190 |
binop = syntax.IN // IN token is out of order |
| 191 |
} |
| 192 |
y := stack[sp-1] |
| 193 |
x := stack[sp-2] |
| 194 |
sp -= 2 |
| 195 |
z, err2 := Binary(binop, x, y) |
| 196 |
if err2 != nil { |
| 197 |
err = err2 |
| 198 |
break loop |
| 199 |
} |
| 200 |
stack[sp] = z |
| 201 |
sp++ |
| 202 |
|
| 203 |
case compile.UPLUS, compile.UMINUS, compile.TILDE: |
| 204 |
var unop syntax.Token |
| 205 |
if op == compile.TILDE { |
| 206 |
unop = syntax.TILDE |
| 207 |
} else { |
| 208 |
unop = syntax.Token(op-compile.UPLUS) + syntax.PLUS |
| 209 |
} |
| 210 |
x := stack[sp-1] |
| 211 |
y, err2 := Unary(unop, x) |
| 212 |
if err2 != nil { |
| 213 |
err = err2 |
| 214 |
break loop |
| 215 |
} |
| 216 |
stack[sp-1] = y |
| 217 |
|
| 218 |
case compile.INPLACE_ADD: |
| 219 |
y := stack[sp-1] |
| 220 |
x := stack[sp-2] |
| 221 |
sp -= 2 |
| 222 |
|
| 223 |
// It's possible that y is not Iterable but |
| 224 |
// nonetheless defines x+y, in which case we |
| 225 |
// should fall back to the general case. |
| 226 |
var z Value |
| 227 |
if xlist, ok := x.(*List); ok { |
| 228 |
if yiter, ok := y.(Iterable); ok { |
| 229 |
if err = xlist.checkMutable("apply += to"); err != nil { |
| 230 |
break loop |
| 231 |
} |
| 232 |
listExtend(xlist, yiter) |
| 233 |
z = xlist |
| 234 |
} |
| 235 |
} |
| 236 |
if z == nil { |
| 237 |
z, err = Binary(syntax.PLUS, x, y) |
| 238 |
if err != nil { |
| 239 |
break loop |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
stack[sp] = z |
| 244 |
sp++ |
| 245 |
|
| 246 |
case compile.INPLACE_PIPE: |
| 247 |
y := stack[sp-1] |
| 248 |
x := stack[sp-2] |
| 249 |
sp -= 2 |
| 250 |
|
| 251 |
// It's possible that y is not Dict but |
| 252 |
// nonetheless defines x|y, in which case we |
| 253 |
// should fall back to the general case. |
| 254 |
var z Value |
| 255 |
if xdict, ok := x.(*Dict); ok { |
| 256 |
if ydict, ok := y.(*Dict); ok { |
| 257 |
if err = xdict.ht.checkMutable("apply |= to"); err != nil { |
| 258 |
break loop |
| 259 |
} |
| 260 |
xdict.ht.addAll(&ydict.ht) // can't fail |
| 261 |
z = xdict |
| 262 |
} |
| 263 |
} |
| 264 |
if z == nil { |
| 265 |
z, err = Binary(syntax.PIPE, x, y) |
| 266 |
if err != nil { |
| 267 |
break loop |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
stack[sp] = z |
| 272 |
sp++ |
| 273 |
|
| 274 |
case compile.NONE: |
| 275 |
stack[sp] = None |
| 276 |
sp++ |
| 277 |
|
| 278 |
case compile.TRUE: |
| 279 |
stack[sp] = True |
| 280 |
sp++ |
| 281 |
|
| 282 |
case compile.FALSE: |
| 283 |
stack[sp] = False |
| 284 |
sp++ |
| 285 |
|
| 286 |
case compile.MANDATORY: |
| 287 |
stack[sp] = mandatory{} |
| 288 |
sp++ |
| 289 |
|
| 290 |
case compile.JMP: |
| 291 |
pc = arg |
| 292 |
|
| 293 |
case compile.CALL, compile.CALL_VAR, compile.CALL_KW, compile.CALL_VAR_KW: |
| 294 |
var kwargs Value |
| 295 |
if op == compile.CALL_KW || op == compile.CALL_VAR_KW { |
| 296 |
kwargs = stack[sp-1] |
| 297 |
sp-- |
| 298 |
} |
| 299 |
|
| 300 |
var args Value |
| 301 |
if op == compile.CALL_VAR || op == compile.CALL_VAR_KW { |
| 302 |
args = stack[sp-1] |
| 303 |
sp-- |
| 304 |
} |
| 305 |
|
| 306 |
// named args (pairs) |
| 307 |
var kvpairs []Tuple |
| 308 |
if nkvpairs := int(arg & 0xff); nkvpairs > 0 { |
| 309 |
kvpairs = make([]Tuple, 0, nkvpairs) |
| 310 |
kvpairsAlloc := make(Tuple, 2*nkvpairs) // allocate a single backing array |
| 311 |
sp -= 2 * nkvpairs |
| 312 |
for i := range nkvpairs { |
| 313 |
pair := kvpairsAlloc[:2:2] |
| 314 |
kvpairsAlloc = kvpairsAlloc[2:] |
| 315 |
pair[0] = stack[sp+2*i] // name |
| 316 |
pair[1] = stack[sp+2*i+1] // value |
| 317 |
kvpairs = append(kvpairs, pair) |
| 318 |
} |
| 319 |
} |
| 320 |
if kwargs != nil { |
| 321 |
// Add key/value items from **kwargs dictionary. |
| 322 |
dict, ok := kwargs.(IterableMapping) |
| 323 |
if !ok { |
| 324 |
err = fmt.Errorf("argument after ** must be a mapping, not %s", kwargs.Type()) |
| 325 |
break loop |
| 326 |
} |
| 327 |
items := dict.Items() |
| 328 |
for _, item := range items { |
| 329 |
if !is[String](item[0]) { |
| 330 |
err = fmt.Errorf("keywords must be strings, not %s", item[0].Type()) |
| 331 |
break loop |
| 332 |
} |
| 333 |
} |
| 334 |
if len(kvpairs) == 0 { |
| 335 |
kvpairs = items |
| 336 |
} else { |
| 337 |
kvpairs = append(kvpairs, items...) |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
// positional args |
| 342 |
var positional Tuple |
| 343 |
if npos := int(arg >> 8); npos > 0 { |
| 344 |
positional = stack[sp-npos : sp] |
| 345 |
sp -= npos |
| 346 |
|
| 347 |
// Copy positional arguments into a new array, |
| 348 |
// unless the callee is another Starlark function, |
| 349 |
// in which case it can be trusted not to mutate them. |
| 350 |
if !is[*Function](stack[sp-1]) || args != nil { |
| 351 |
positional = slices.Clone(positional) |
| 352 |
} |
| 353 |
} |
| 354 |
if args != nil { |
| 355 |
// Add elements from *args sequence. |
| 356 |
iter := Iterate(args) |
| 357 |
if iter == nil { |
| 358 |
err = fmt.Errorf("argument after * must be iterable, not %s", args.Type()) |
| 359 |
break loop |
| 360 |
} |
| 361 |
var elem Value |
| 362 |
for iter.Next(&elem) { |
| 363 |
positional = append(positional, elem) |
| 364 |
} |
| 365 |
iter.Done() |
| 366 |
} |
| 367 |
|
| 368 |
function := stack[sp-1] |
| 369 |
|
| 370 |
if vmdebug { |
| 371 |
fmt.Printf("VM call %s args=%s kwargs=%s @%s\n", |
| 372 |
function, positional, kvpairs, f.Position(fr.pc)) |
| 373 |
} |
| 374 |
|
| 375 |
thread.endProfSpan() |
| 376 |
z, err2 := Call(thread, function, positional, kvpairs) |
| 377 |
thread.beginProfSpan() |
| 378 |
if err2 != nil { |
| 379 |
err = err2 |
| 380 |
break loop |
| 381 |
} |
| 382 |
if vmdebug { |
| 383 |
fmt.Printf("Resuming %s @ %s\n", f.Name, f.Position(0)) |
| 384 |
} |
| 385 |
stack[sp-1] = z |
| 386 |
|
| 387 |
case compile.ITERPUSH: |
| 388 |
x := stack[sp-1] |
| 389 |
sp-- |
| 390 |
iter := Iterate(x) |
| 391 |
if iter == nil { |
| 392 |
err = fmt.Errorf("%s value is not iterable", x.Type()) |
| 393 |
break loop |
| 394 |
} |
| 395 |
iterstack = append(iterstack, iter) |
| 396 |
|
| 397 |
case compile.ITERJMP: |
| 398 |
iter := iterstack[len(iterstack)-1] |
| 399 |
if iter.Next(&stack[sp]) { |
| 400 |
sp++ |
| 401 |
} else { |
| 402 |
pc = arg |
| 403 |
} |
| 404 |
|
| 405 |
case compile.ITERPOP: |
| 406 |
n := len(iterstack) - 1 |
| 407 |
iterstack[n].Done() |
| 408 |
iterstack = iterstack[:n] |
| 409 |
|
| 410 |
case compile.NOT: |
| 411 |
stack[sp-1] = !stack[sp-1].Truth() |
| 412 |
|
| 413 |
case compile.RETURN: |
| 414 |
result = stack[sp-1] |
| 415 |
break loop |
| 416 |
|
| 417 |
case compile.SETINDEX: |
| 418 |
z := stack[sp-1] |
| 419 |
y := stack[sp-2] |
| 420 |
x := stack[sp-3] |
| 421 |
sp -= 3 |
| 422 |
err = setIndex(x, y, z) |
| 423 |
if err != nil { |
| 424 |
break loop |
| 425 |
} |
| 426 |
|
| 427 |
case compile.INDEX: |
| 428 |
y := stack[sp-1] |
| 429 |
x := stack[sp-2] |
| 430 |
sp -= 2 |
| 431 |
z, err2 := getIndex(x, y) |
| 432 |
if err2 != nil { |
| 433 |
err = err2 |
| 434 |
break loop |
| 435 |
} |
| 436 |
stack[sp] = z |
| 437 |
sp++ |
| 438 |
|
| 439 |
case compile.ATTR: |
| 440 |
x := stack[sp-1] |
| 441 |
name := f.Prog.Names[arg] |
| 442 |
y, err2 := getAttr(x, name) |
| 443 |
if err2 != nil { |
| 444 |
err = err2 |
| 445 |
break loop |
| 446 |
} |
| 447 |
stack[sp-1] = y |
| 448 |
|
| 449 |
case compile.SETFIELD: |
| 450 |
y := stack[sp-1] |
| 451 |
x := stack[sp-2] |
| 452 |
sp -= 2 |
| 453 |
name := f.Prog.Names[arg] |
| 454 |
if err2 := setField(x, name, y); err2 != nil { |
| 455 |
err = err2 |
| 456 |
break loop |
| 457 |
} |
| 458 |
|
| 459 |
case compile.MAKEDICT: |
| 460 |
stack[sp] = new(Dict) |
| 461 |
sp++ |
| 462 |
|
| 463 |
case compile.SETDICT, compile.SETDICTUNIQ: |
| 464 |
dict := stack[sp-3].(*Dict) |
| 465 |
k := stack[sp-2] |
| 466 |
v := stack[sp-1] |
| 467 |
sp -= 3 |
| 468 |
oldlen := dict.Len() |
| 469 |
if err2 := dict.SetKey(k, v); err2 != nil { |
| 470 |
err = err2 |
| 471 |
break loop |
| 472 |
} |
| 473 |
if op == compile.SETDICTUNIQ && dict.Len() == oldlen { |
| 474 |
err = fmt.Errorf("duplicate key: %v", k) |
| 475 |
break loop |
| 476 |
} |
| 477 |
|
| 478 |
case compile.APPEND: |
| 479 |
elem := stack[sp-1] |
| 480 |
list := stack[sp-2].(*List) |
| 481 |
sp -= 2 |
| 482 |
list.elems = append(list.elems, elem) |
| 483 |
|
| 484 |
case compile.SLICE: |
| 485 |
x := stack[sp-4] |
| 486 |
lo := stack[sp-3] |
| 487 |
hi := stack[sp-2] |
| 488 |
step := stack[sp-1] |
| 489 |
sp -= 4 |
| 490 |
res, err2 := slice(x, lo, hi, step) |
| 491 |
if err2 != nil { |
| 492 |
err = err2 |
| 493 |
break loop |
| 494 |
} |
| 495 |
stack[sp] = res |
| 496 |
sp++ |
| 497 |
|
| 498 |
case compile.UNPACK: |
| 499 |
n := int(arg) |
| 500 |
iterable := stack[sp-1] |
| 501 |
sp-- |
| 502 |
iter := Iterate(iterable) |
| 503 |
if iter == nil { |
| 504 |
err = fmt.Errorf("got %s in sequence assignment", iterable.Type()) |
| 505 |
break loop |
| 506 |
} |
| 507 |
i := 0 |
| 508 |
sp += n |
| 509 |
for i < n && iter.Next(&stack[sp-1-i]) { |
| 510 |
i++ |
| 511 |
} |
| 512 |
var dummy Value |
| 513 |
if iter.Next(&dummy) { |
| 514 |
// NB: Len may return -1 here in obscure cases. |
| 515 |
err = fmt.Errorf("too many values to unpack (got %d, want %d)", Len(iterable), n) |
| 516 |
break loop |
| 517 |
} |
| 518 |
iter.Done() |
| 519 |
if i < n { |
| 520 |
err = fmt.Errorf("too few values to unpack (got %d, want %d)", i, n) |
| 521 |
break loop |
| 522 |
} |
| 523 |
|
| 524 |
case compile.CJMP: |
| 525 |
if stack[sp-1].Truth() { |
| 526 |
pc = arg |
| 527 |
} |
| 528 |
sp-- |
| 529 |
|
| 530 |
case compile.CONSTANT: |
| 531 |
stack[sp] = fn.module.constants[arg] |
| 532 |
sp++ |
| 533 |
|
| 534 |
case compile.MAKETUPLE: |
| 535 |
n := int(arg) |
| 536 |
tuple := make(Tuple, n) |
| 537 |
sp -= n |
| 538 |
copy(tuple, stack[sp:]) |
| 539 |
stack[sp] = tuple |
| 540 |
sp++ |
| 541 |
|
| 542 |
case compile.MAKELIST: |
| 543 |
n := int(arg) |
| 544 |
elems := make([]Value, n) |
| 545 |
sp -= n |
| 546 |
copy(elems, stack[sp:]) |
| 547 |
stack[sp] = NewList(elems) |
| 548 |
sp++ |
| 549 |
|
| 550 |
case compile.MAKEFUNC: |
| 551 |
funcode := f.Prog.Functions[arg] |
| 552 |
tuple := stack[sp-1].(Tuple) |
| 553 |
n := len(tuple) - len(funcode.FreeVars) |
| 554 |
defaults := tuple[:n:n] |
| 555 |
freevars := tuple[n:] |
| 556 |
stack[sp-1] = &Function{ |
| 557 |
funcode: funcode, |
| 558 |
module: fn.module, |
| 559 |
defaults: defaults, |
| 560 |
freevars: freevars, |
| 561 |
} |
| 562 |
|
| 563 |
case compile.LOAD: |
| 564 |
n := int(arg) |
| 565 |
module := string(stack[sp-1].(String)) |
| 566 |
sp-- |
| 567 |
|
| 568 |
if thread.Load == nil { |
| 569 |
err = fmt.Errorf("load not implemented by this application") |
| 570 |
break loop |
| 571 |
} |
| 572 |
|
| 573 |
thread.endProfSpan() |
| 574 |
dict, err2 := thread.Load(thread, module) |
| 575 |
thread.beginProfSpan() |
| 576 |
if err2 != nil { |
| 577 |
err = wrappedError{ |
| 578 |
msg: fmt.Sprintf("cannot load %s: %v", module, err2), |
| 579 |
cause: err2, |
| 580 |
} |
| 581 |
break loop |
| 582 |
} |
| 583 |
|
| 584 |
for i := range n { |
| 585 |
from := string(stack[sp-1-i].(String)) |
| 586 |
v, ok := dict[from] |
| 587 |
if !ok { |
| 588 |
err = fmt.Errorf("load: name %s not found in module %s", from, module) |
| 589 |
if n := spell.Nearest(from, dict.Keys()); n != "" { |
| 590 |
err = fmt.Errorf("%s (did you mean %s?)", err, n) |
| 591 |
} |
| 592 |
break loop |
| 593 |
} |
| 594 |
stack[sp-1-i] = v |
| 595 |
} |
| 596 |
|
| 597 |
case compile.SETLOCAL: |
| 598 |
locals[arg] = stack[sp-1] |
| 599 |
sp-- |
| 600 |
|
| 601 |
case compile.SETLOCALCELL: |
| 602 |
locals[arg].(*cell).v = stack[sp-1] |
| 603 |
sp-- |
| 604 |
|
| 605 |
case compile.SETGLOBAL: |
| 606 |
fn.module.globals[arg] = stack[sp-1] |
| 607 |
sp-- |
| 608 |
|
| 609 |
case compile.LOCAL: |
| 610 |
x := locals[arg] |
| 611 |
if x == nil { |
| 612 |
err = fmt.Errorf("local variable %s referenced before assignment", f.Locals[arg].Name) |
| 613 |
break loop |
| 614 |
} |
| 615 |
stack[sp] = x |
| 616 |
sp++ |
| 617 |
|
| 618 |
case compile.FREE: |
| 619 |
stack[sp] = fn.freevars[arg] |
| 620 |
sp++ |
| 621 |
|
| 622 |
case compile.LOCALCELL: |
| 623 |
v := locals[arg].(*cell).v |
| 624 |
if v == nil { |
| 625 |
err = fmt.Errorf("local variable %s referenced before assignment", f.Locals[arg].Name) |
| 626 |
break loop |
| 627 |
} |
| 628 |
stack[sp] = v |
| 629 |
sp++ |
| 630 |
|
| 631 |
case compile.FREECELL: |
| 632 |
v := fn.freevars[arg].(*cell).v |
| 633 |
if v == nil { |
| 634 |
err = fmt.Errorf("local variable %s referenced before assignment", f.FreeVars[arg].Name) |
| 635 |
break loop |
| 636 |
} |
| 637 |
stack[sp] = v |
| 638 |
sp++ |
| 639 |
|
| 640 |
case compile.GLOBAL: |
| 641 |
x := fn.module.globals[arg] |
| 642 |
if x == nil { |
| 643 |
err = fmt.Errorf("global variable %s referenced before assignment", f.Prog.Globals[arg].Name) |
| 644 |
break loop |
| 645 |
} |
| 646 |
stack[sp] = x |
| 647 |
sp++ |
| 648 |
|
| 649 |
case compile.PREDECLARED: |
| 650 |
name := f.Prog.Names[arg] |
| 651 |
x := fn.module.predeclared[name] |
| 652 |
if x == nil { |
| 653 |
err = fmt.Errorf("internal error: predeclared variable %s is uninitialized", name) |
| 654 |
break loop |
| 655 |
} |
| 656 |
stack[sp] = x |
| 657 |
sp++ |
| 658 |
|
| 659 |
case compile.UNIVERSAL: |
| 660 |
stack[sp] = Universe[f.Prog.Names[arg]] |
| 661 |
sp++ |
| 662 |
|
| 663 |
default: |
| 664 |
err = fmt.Errorf("unimplemented: %s", op) |
| 665 |
break loop |
| 666 |
} |
| 667 |
} |
| 668 |
// (deferred cleanup runs here) |
| 669 |
return result, err |
| 670 |
} |
| 671 |
|
| 672 |
type wrappedError struct { |
| 673 |
msg string |
| 674 |
cause error |
| 675 |
} |
| 676 |
|
| 677 |
func (e wrappedError) Error() string { |
| 678 |
return e.msg |
| 679 |
} |
| 680 |
|
| 681 |
// Implements the xerrors.Wrapper interface |
| 682 |
// https://godoc.org/golang.org/x/xerrors#Wrapper |
| 683 |
func (e wrappedError) Unwrap() error { |
| 684 |
return e.cause |
| 685 |
} |
| 686 |
|
| 687 |
// mandatory is a sentinel value used in a function's defaults tuple |
| 688 |
// to indicate that a (keyword-only) parameter is mandatory. |
| 689 |
type mandatory struct{} |
| 690 |
|
| 691 |
func (mandatory) String() string { return "mandatory" } |
| 692 |
func (mandatory) Type() string { return "mandatory" } |
| 693 |
func (mandatory) Freeze() {} // immutable |
| 694 |
func (mandatory) Truth() Bool { return False } |
| 695 |
func (mandatory) Hash() (uint32, error) { return 0, nil } |
| 696 |
|
| 697 |
// A cell is a box containing a Value. |
| 698 |
// Local variables marked as cells hold their value indirectly |
| 699 |
// so that they may be shared by outer and inner nested functions. |
| 700 |
// Cells are always accessed using indirect {FREE,LOCAL,SETLOCAL}CELL instructions. |
| 701 |
// The FreeVars tuple contains only cells. |
| 702 |
// The FREE instruction always yields a cell. |
| 703 |
type cell struct{ v Value } |
| 704 |
|
| 705 |
func (c *cell) String() string { return "cell" } |
| 706 |
func (c *cell) Type() string { return "cell" } |
| 707 |
func (c *cell) Freeze() { |
| 708 |
if c.v != nil { |
| 709 |
c.v.Freeze() |
| 710 |
} |
| 711 |
} |
| 712 |
func (c *cell) Truth() Bool { panic("unreachable") } |
| 713 |
func (c *cell) Hash() (uint32, error) { panic("unreachable") } |