| 1466 | } |
| 1467 | |
| 1468 | func (ls *LState) Replace(idx int, value LValue) { |
| 1469 | base := ls.currentLocalBase() |
| 1470 | if idx > 0 { |
| 1471 | reg := base + idx - 1 |
| 1472 | if reg < ls.reg.Top() { |
| 1473 | ls.reg.Set(reg, value) |
| 1474 | } |
| 1475 | } else if idx == 0 { |
| 1476 | } else if idx > RegistryIndex { |
| 1477 | if tidx := ls.reg.Top() + idx; tidx >= base { |
| 1478 | ls.reg.Set(tidx, value) |
| 1479 | } |
| 1480 | } else { |
| 1481 | switch idx { |
| 1482 | case RegistryIndex: |
| 1483 | if tb, ok := value.(*LTable); ok { |
| 1484 | ls.G.Registry = tb |
| 1485 | } else { |
| 1486 | ls.RaiseError("registry must be a table(%v)", value.Type().String()) |
| 1487 | } |
| 1488 | case EnvironIndex: |
| 1489 | if ls.currentFrame == nil { |
| 1490 | ls.RaiseError("no calling environment") |
| 1491 | } |
| 1492 | if tb, ok := value.(*LTable); ok { |
| 1493 | ls.currentFrame.Fn.Env = tb |
| 1494 | } else { |
| 1495 | ls.RaiseError("environment must be a table(%v)", value.Type().String()) |
| 1496 | } |
| 1497 | case GlobalsIndex: |
| 1498 | if tb, ok := value.(*LTable); ok { |
| 1499 | ls.G.Global = tb |
| 1500 | } else { |
| 1501 | ls.RaiseError("_G must be a table(%v)", value.Type().String()) |
| 1502 | } |
| 1503 | default: |
| 1504 | fn := ls.currentFrame.Fn |
| 1505 | index := GlobalsIndex - idx - 1 |
| 1506 | if index < len(fn.Upvalues) { |
| 1507 | fn.Upvalues[index].SetValue(value) |
| 1508 | } |
| 1509 | } |
| 1510 | } |
| 1511 | } |
| 1512 | |
| 1513 | func (ls *LState) Get(idx int) LValue { |
| 1514 | base := ls.currentLocalBase() |