module scheme import reader // Environments share the VM's stable absolute global slots. Report/null spaces // are cached and immutable; explicit mutable spaces support persistent library // state and shared-location imports without a second runtime or value format. @[heap] struct Namespace { implicit_syntax bool = true // Immutable report names initially encode a pristine standard slot as // -slot-2. Only an explicit import materializes a shared absolute location. report_owner &Owner = unsafe { nil } mut: writable bool bindings map[string]int syntax &SyntaxBindings = unsafe { nil } keyword_variables []string imported map[string]bool // Allocated lazily from an existing VM-unique compilation coordinate. syntax_identity u64 syntax_locations map[string]&SyntaxGlobalLocation } @[heap] struct EvalEnvironment { owner &Owner space &Namespace = unsafe { nil } } @[heap] struct EvalState { mut: interaction Value report Value null_space Value } fn (mut vm VM) eval_state() &EvalState { if isnil(vm.evaluation) { vm.evaluation = &vm.evaluation_storage } return vm.evaluation } // Every assignment targets a resolved binding location, including assignments // through aliases. Observe hook changes here so a warm dispatch needs no weak // lookups and a resumed method factory sees intervening rebindings. fn (mut vm VM) replace_global(index int, value Value) { vm.ensure_goops_slot(index) if index < vm.standard_count && index !in vm.standard_original { vm.standard_original[index] = vm.globals[index] } if !isnil(vm.goops) && vm.goops.protocols_ready && (index == vm.goops.applicability_binding || index == vm.goops.effective_binding || index == vm.goops.specialized_binding) && !eqv(vm.globals[index], value) { vm.goops.applicability_epoch++ } container_store(mut vm.globals, index, value) vm.invalidate_native_bindings() } fn (vm &VM) environment_space(value Value) !&Namespace { if value.tag != .environment { return failure('expected an environment specifier') } environment := unsafe { &EvalEnvironment(value.pointer()) } if voidptr(environment.owner) != voidptr(vm.owner) { return failure('environment belongs to another VM') } return environment.space } pub fn (mut vm VM) interaction_environment() !Value { vm.ready()! mut state := vm.eval_state() if state.interaction.tag != .environment { value := payload_value(.environment, &EvalEnvironment{ owner: vm.owner }) if v_arena_current() != vm.allocation_owner { return value } state.interaction = value } return state.interaction } pub fn (mut vm VM) report_environment(version int) !Value { vm.ready()! if version != 5 { return failure('unsupported Scheme report version') } mut state := vm.eval_state() if state.report.tag != .environment { mut space := &Namespace{ report_owner: vm.owner } // Before root imports, every original name is still present, including // redefinitions. Avoid a second map-key iteration (and its implicit // string clones) in this common case. Imported aliases need filtering. mut count := vm.standard_count if vm.imported.len > 0 { count = 0 for name, index in *vm.bindings { if index < vm.standard_count && !vm.imported[name] { count++ } } } if u64(count) <= u64(~u32(0)) { space.bindings.reserve(u32(count)) } for name, index in *vm.bindings { if index >= vm.standard_count || vm.imported[name] { continue } space.bindings[name] = -index - 2 } value := payload_value(.environment, &EvalEnvironment{ owner: vm.owner, space: space }) if v_arena_current() != vm.allocation_owner { return value } state.report = value } return state.report } fn report_slot(space &Namespace, slot int) bool { return !isnil(space) && !isnil(space.report_owner) && slot <= -2 } fn (vm &VM) pristine_standard(slot int) Value { if vm.globals[slot].tag == .uninitialized { vm.ensure_goops_slot(slot) } return vm.standard_original[slot] or { vm.globals[slot] } } pub fn (mut vm VM) null_environment(version int) !Value { vm.ready()! if version != 5 { return failure('unsupported Scheme report version') } mut state := vm.eval_state() if state.null_space.tag != .environment { value := payload_value(.environment, &EvalEnvironment{ owner: vm.owner, space: &Namespace{} }) if v_arena_current() != vm.allocation_owner { return value } state.null_space = value } return state.null_space } // Only a cache miss resolves a name. Keep namespace/map lookup out of the // common instruction dispatcher, independently of GCC inlining heuristics. @[noinline] fn (vm &VM) global_index_in(space &Namespace, name string) !int { if isnil(space) { return vm.global_index(name) } if index := space.bindings[name] { return index } return failure('unbound variable ${binding_diagnostic_name(name)}') } fn (mut vm VM) bind_global_in(space &Namespace, name string, value Value) !int { if !isnil(space) && !space.writable { return failure('environment does not permit global definitions') } if (isnil(space) && name in vm.imported) || (!isnil(space) && name in space.imported) { return failure('cannot redefine imported binding ${name}') } if !isnil(vm.owner.bounded_files) && !special_name(name) { syntax := if isnil(space) { vm.syntax } else { space.syntax } if isnil(syntax) || name !in syntax.entries { return vm.owner.bounded_files.publish_binding(mut vm, space, name, value) } } if isnil(space) { return vm.bind_global(name, value) } mut target := unsafe { &Namespace(space) } if !isnil(target.syntax) && name in target.syntax.entries { target.syntax.entries.delete(name) vm.binding_epoch++ } if special_name(name) && name !in target.keyword_variables { target.keyword_variables << name } if index := target.bindings[name] { vm.replace_global(index, value) vm.note_syntax_location(space, name, SyntaxBinding{ kind: .global, name: name, namespace: space, global_slot: index }) return index } index := vm.globals.len vm.globals << value target.bindings[name] = index vm.binding_epoch++ vm.note_syntax_location(space, name, SyntaxBinding{ kind: .global, name: name, namespace: space, global_slot: index }) return index } // The declaration already established the public location. A private alias // exposes that same slot to its exact captured syntax context, without another // value allocation or an imported/read-only binding. fn (mut vm VM) bind_global_alias_in(space &Namespace, name string, slot int) ! { vm.check_assignment_in(space, name)! if isnil(space) { if existing := (*vm.bindings)[name] { if existing == slot { return } } (*vm.bindings)[name] = slot } else { mut target := unsafe { &Namespace(space) } if existing := target.bindings[name] { if existing == slot { return } } target.bindings[name] = slot } vm.binding_epoch++ vm.note_syntax_location(space, name, SyntaxBinding{ kind: .global, name: name, namespace: space, global_slot: slot }) } fn (vm &VM) check_assignment_in(space &Namespace, name string) ! { if !isnil(space) && !space.writable { return failure('environment does not permit global assignment') } vm.check_imported_assignment(space, name)! } fn (vm &VM) assignment_permitted_in(space &Namespace, name string) bool { return if isnil(space) { name !in vm.imported } else { space.writable && name !in space.imported } } fn (vm &VM) check_imported_assignment(space &Namespace, name string) ! { if isnil(space) { if name in vm.imported { return failure('cannot assign imported binding ${name}') } return } if name in space.imported { return failure('cannot assign imported binding ${name}') } } fn (mut vm VM) bind_syntax_in(space &Namespace, name string, transformer &SyntaxTransformer) ! { if isnil(space) { if name in vm.imported { return failure('cannot redefine imported binding ${name}') } vm.bind_syntax(name, transformer) return } if !space.writable { return failure('environment does not permit global syntax definitions') } if name in space.imported { return failure('cannot redefine imported binding ${name}') } mut target := unsafe { &Namespace(space) } if isnil(target.syntax) { target.syntax = &SyntaxBindings{} } binding := SyntaxBinding{ kind: .transformer, name: name, transformer: transformer, top_level: true, namespace: space } target.syntax.entries[name] = binding vm.binding_epoch++ vm.note_syntax_location(space, name, binding) } fn (vm &VM) new_code(doc reader.Document, seed u64, space &Namespace, source_path string) &Code { mut code := &Code{ doc: doc, namespace: space, source_path: source_path, syntax_seed: seed, expansion_limit: vm.options.max_expansions, compiler_vm: vm } if doc.arena.has_labels() { code.enable_datum_graphs() } code.known_globals = if isnil(space) { vm.bindings } else { &space.bindings } if isnil(space) { code.keyword_variables = vm.keyword_variables.clone() if !isnil(vm.syntax) && vm.syntax.entries.len > 0 { // Borrow the stable syntax-table object while compiling. Procedural // transformer calls may update its entries; generated.globals is a private overlay. code.syntax_base = vm.syntax code.has_syntax = true } } else { code.keyword_variables = space.keyword_variables.clone() if !isnil(space.syntax) && space.syntax.entries.len > 0 { code.syntax_base = space.syntax code.has_syntax = true } } return code }