module scheme import reader import sync // The built-in protocols are compiled in their fixed root syntax once. Each // VM installs its own binding caches, closed procedures and method objects. struct GoopsProtocolCache { lock &sync.SpinLock mut: program &Function = unsafe { nil } } const goops_protocol_cache = &GoopsProtocolCache{ lock: sync.new_spin_lock() } fn goops_protocol_program() &Function { mut cache := goops_protocol_cache cache.lock.lock() defer { cache.lock.unlock() } if isnil(cache.program) { // A process-wide immutable template must never borrow a VM's arena. previous := unsafe { v_arena_suspend() } defer { unsafe { v_arena_resume(previous) } } cache.program = compile_goops_protocol() } return cache.program } fn compile_goops_protocol() &Function { doc := reader.read_all($embed_file('goops_protocol.scm').to_string()) or { panic('cannot read GOOPS protocols: ${err}') } return compile_program(&Code{ doc: doc, builtin_root: true }, doc.forms) } fn instantiate_goops_function(template &Function, code &Code) &Function { mut children := []&Function{cap: template.children.len} for child in template.children { children << instantiate_goops_function(child, code) } mut function := &Function{ ...*template code: code global_slots: template.global_slots.clone() children: children } // Resolving a forward or hygienic reference can patch its instruction and // memoize a location in its constant. Keep those writes local to the VM. mut copied := false for i, value in template.constants { if value.tag != .global_reference { continue } if !copied { function.instructions = template.instructions.clone() function.constants = template.constants.clone() copied = true } reference := unsafe { &GlobalReference(value.pointer()) } container_store_validated(mut function.constants, i, payload_value(.global_reference, &GlobalReference{ ...*reference })) } return function } const goops_protocol_descriptors = [ NativeDescriptor{'%goops-allocate', goops_allocate_native} NativeDescriptor{'compute-std-cpl', goops_std_cpl_native} NativeDescriptor{'%goops-std-slots', goops_std_slots_native} NativeDescriptor{'%goops-direct-initargs', goops_direct_initargs_native} NativeDescriptor{'get-keyword', goops_get_keyword_native} NativeDescriptor{'%goops-unbound?', goops_unbound_native} NativeDescriptor{'%goops-validate-initargs', goops_validate_initargs_native} NativeDescriptor{'%goops-configure-slot!', goops_configure_slot_native} NativeDescriptor{'goops-error', goops_goops_error_native} NativeDescriptor{'%goops-field-accessor', goops_field_accessor_native} NativeDescriptor{'%goops-slot-accessor-target', goops_slot_accessor_target_native} NativeDescriptor{'%goops-add-default!', goops_add_default_native} NativeDescriptor{'%goops-normalize-metas', goops_normalize_metas_native} NativeDescriptor{'%goops-common-meta', goops_common_meta_native} NativeDescriptor{'%goops-cache-meta!', goops_cache_meta_native} NativeDescriptor{'%goops-finish-class!', goops_finish_class_native} NativeDescriptor{'%goops-enable-protocols!', goops_enable_protocols_native} NativeDescriptor{'%goops-swap-instances!', goops_swap_instances_native} NativeDescriptor{'%goops-commit-instance!', goops_commit_instance_native} NativeDescriptor{'%goops-prepare-copy', goops_prepare_copy_native} NativeDescriptor{'%goops-bind-copy', goops_bind_copy_native} NativeDescriptor{'%goops-copy-winder', goops_copy_winder_native} NativeDescriptor{'%goops-copy-target', goops_copy_target_native} NativeDescriptor{'%goops-clone-slot?', goops_clone_slot_native} NativeDescriptor{'%goops-copy-native-slot!', goops_copy_native_slot_native} NativeDescriptor{'%goops-redefinition-preflight', goops_redefinition_preflight_native} NativeDescriptor{'%goops-redefine-class!', goops_redefine_class_native} NativeDescriptor{'%goops-obsolete-class', goops_obsolete_native} NativeDescriptor{'%goops-slot-accessors', goops_low_accessors_native} NativeDescriptor{'%goops-share-slot!', goops_share_slot_native} NativeDescriptor{'%goops-inherit-slot!', goops_inherit_slot_native} NativeDescriptor{'%goops-static-slot?', goops_static_slot_native} NativeDescriptor{'%goops-install-slot!', goops_install_slot_native} NativeDescriptor{'%goops-getter-method', goops_getter_method_native} NativeDescriptor{'%goops-setter-method', goops_setter_method_native} NativeDescriptor{'%compute-applicable-methods', goops_compute_methods_native} NativeDescriptor{'%goops-more-specific?', goops_more_specific_native} NativeDescriptor{'%goops-sort-methods', goops_sort_methods_native} NativeDescriptor{'find-method', goops_find_method_native} NativeDescriptor{'generic?', goops_generic_predicate_native} NativeDescriptor{'ensure-generic', goops_ensure_generic_native} NativeDescriptor{'ensure-accessor', goops_ensure_accessor_native} NativeDescriptor{'primitive-generic-generic', goops_primitive_generic_native} NativeDescriptor{'enable-primitive-generic!', goops_enable_primitives_native}, ]! fn (mut vm VM) install_goops_protocols() { // Installing the trusted protocol definitions is construction work, not a // caller evaluation. Keep it bounded by the normal defaults, then restore // every caller option before the VM can escape to its host. previous := vm.options defaults := Options{} vm.options = Options{ ...previous max_steps: defaults.max_steps max_frames: defaults.max_frames max_expansions: defaults.max_expansions } defer { vm.options = previous } vm.install_goops_metaobjects() for index in 0 .. goops_protocol_descriptors.len { entry := &goops_protocol_descriptors[index] vm.bind_global(entry.name, procedure_value(&Procedure{ owner: vm.owner mode: .native name: entry.name native: entry.callback })) } vm.bind_global('%goops-unbound', vm.goops.unbound) context := vm.next_context() program := goops_protocol_program() code := &Code{ ...*program.code allocation_owner: vm.allocation_owner syntax_seed: context builtin_root: false } vm.run_program(instantiate_goops_function(program, code), context) or { panic('cannot bootstrap GOOPS protocols: ${err}') } }