| 1 | module scheme |
| 2 | |
| 3 | import reader |
| 4 | import sync |
| 5 | |
| 6 | // The built-in protocols are compiled in their fixed root syntax once. Each |
| 7 | // VM installs its own binding caches, closed procedures and method objects. |
| 8 | struct GoopsProtocolCache { |
| 9 | lock &sync.SpinLock |
| 10 | mut: |
| 11 | program &Function = unsafe { nil } |
| 12 | } |
| 13 | |
| 14 | const goops_protocol_cache = &GoopsProtocolCache{ lock: sync.new_spin_lock() } |
| 15 | |
| 16 | fn goops_protocol_program() &Function { |
| 17 | mut cache := goops_protocol_cache |
| 18 | cache.lock.lock() |
| 19 | defer { cache.lock.unlock() } |
| 20 | if isnil(cache.program) { |
| 21 | // A process-wide immutable template must never borrow a VM's arena. |
| 22 | previous := unsafe { v_arena_suspend() } |
| 23 | defer { unsafe { v_arena_resume(previous) } } |
| 24 | cache.program = compile_goops_protocol() |
| 25 | } |
| 26 | return cache.program |
| 27 | } |
| 28 | |
| 29 | fn compile_goops_protocol() &Function { |
| 30 | doc := reader.read_all($embed_file('goops_protocol.scm').to_string()) or { |
| 31 | panic('cannot read GOOPS protocols: ${err}') |
| 32 | } |
| 33 | return compile_program(&Code{ doc: doc, builtin_root: true }, doc.forms) |
| 34 | } |
| 35 | |
| 36 | fn instantiate_goops_function(template &Function, code &Code) &Function { |
| 37 | mut children := []&Function{cap: template.children.len} |
| 38 | for child in template.children { |
| 39 | children << instantiate_goops_function(child, code) |
| 40 | } |
| 41 | mut function := &Function{ |
| 42 | ...*template |
| 43 | code: code |
| 44 | global_slots: template.global_slots.clone() |
| 45 | children: children |
| 46 | } |
| 47 | // Resolving a forward or hygienic reference can patch its instruction and |
| 48 | // memoize a location in its constant. Keep those writes local to the VM. |
| 49 | mut copied := false |
| 50 | for i, value in template.constants { |
| 51 | if value.tag != .global_reference { continue } |
| 52 | if !copied { |
| 53 | function.instructions = template.instructions.clone() |
| 54 | function.constants = template.constants.clone() |
| 55 | copied = true |
| 56 | } |
| 57 | reference := unsafe { &GlobalReference(value.pointer()) } |
| 58 | container_store_validated(mut function.constants, i, |
| 59 | payload_value(.global_reference, &GlobalReference{ ...*reference })) |
| 60 | } |
| 61 | return function |
| 62 | } |
| 63 | |
| 64 | const goops_protocol_descriptors = [ |
| 65 | NativeDescriptor{'%goops-allocate', goops_allocate_native} |
| 66 | NativeDescriptor{'compute-std-cpl', goops_std_cpl_native} |
| 67 | NativeDescriptor{'%goops-std-slots', goops_std_slots_native} |
| 68 | NativeDescriptor{'%goops-direct-initargs', goops_direct_initargs_native} |
| 69 | NativeDescriptor{'get-keyword', goops_get_keyword_native} |
| 70 | NativeDescriptor{'%goops-unbound?', goops_unbound_native} |
| 71 | NativeDescriptor{'%goops-validate-initargs', goops_validate_initargs_native} |
| 72 | NativeDescriptor{'%goops-configure-slot!', goops_configure_slot_native} |
| 73 | NativeDescriptor{'goops-error', goops_goops_error_native} |
| 74 | NativeDescriptor{'%goops-field-accessor', goops_field_accessor_native} |
| 75 | NativeDescriptor{'%goops-slot-accessor-target', goops_slot_accessor_target_native} |
| 76 | NativeDescriptor{'%goops-add-default!', goops_add_default_native} |
| 77 | NativeDescriptor{'%goops-normalize-metas', goops_normalize_metas_native} |
| 78 | NativeDescriptor{'%goops-common-meta', goops_common_meta_native} |
| 79 | NativeDescriptor{'%goops-cache-meta!', goops_cache_meta_native} |
| 80 | NativeDescriptor{'%goops-finish-class!', goops_finish_class_native} |
| 81 | NativeDescriptor{'%goops-enable-protocols!', goops_enable_protocols_native} |
| 82 | NativeDescriptor{'%goops-swap-instances!', goops_swap_instances_native} |
| 83 | NativeDescriptor{'%goops-commit-instance!', goops_commit_instance_native} |
| 84 | NativeDescriptor{'%goops-prepare-copy', goops_prepare_copy_native} |
| 85 | NativeDescriptor{'%goops-bind-copy', goops_bind_copy_native} |
| 86 | NativeDescriptor{'%goops-copy-winder', goops_copy_winder_native} |
| 87 | NativeDescriptor{'%goops-copy-target', goops_copy_target_native} |
| 88 | NativeDescriptor{'%goops-clone-slot?', goops_clone_slot_native} |
| 89 | NativeDescriptor{'%goops-copy-native-slot!', goops_copy_native_slot_native} |
| 90 | NativeDescriptor{'%goops-redefinition-preflight', goops_redefinition_preflight_native} |
| 91 | NativeDescriptor{'%goops-redefine-class!', goops_redefine_class_native} |
| 92 | NativeDescriptor{'%goops-obsolete-class', goops_obsolete_native} |
| 93 | NativeDescriptor{'%goops-slot-accessors', goops_low_accessors_native} |
| 94 | NativeDescriptor{'%goops-share-slot!', goops_share_slot_native} |
| 95 | NativeDescriptor{'%goops-inherit-slot!', goops_inherit_slot_native} |
| 96 | NativeDescriptor{'%goops-static-slot?', goops_static_slot_native} |
| 97 | NativeDescriptor{'%goops-install-slot!', goops_install_slot_native} |
| 98 | NativeDescriptor{'%goops-getter-method', goops_getter_method_native} |
| 99 | NativeDescriptor{'%goops-setter-method', goops_setter_method_native} |
| 100 | NativeDescriptor{'%compute-applicable-methods', goops_compute_methods_native} |
| 101 | NativeDescriptor{'%goops-more-specific?', goops_more_specific_native} |
| 102 | NativeDescriptor{'%goops-sort-methods', goops_sort_methods_native} |
| 103 | NativeDescriptor{'find-method', goops_find_method_native} |
| 104 | NativeDescriptor{'generic?', goops_generic_predicate_native} |
| 105 | NativeDescriptor{'ensure-generic', goops_ensure_generic_native} |
| 106 | NativeDescriptor{'ensure-accessor', goops_ensure_accessor_native} |
| 107 | NativeDescriptor{'primitive-generic-generic', goops_primitive_generic_native} |
| 108 | NativeDescriptor{'enable-primitive-generic!', goops_enable_primitives_native}, |
| 109 | ]! |
| 110 | |
| 111 | fn (mut vm VM) install_goops_protocols() { |
| 112 | // Installing the trusted protocol definitions is construction work, not a |
| 113 | // caller evaluation. Keep it bounded by the normal defaults, then restore |
| 114 | // every caller option before the VM can escape to its host. |
| 115 | previous := vm.options |
| 116 | defaults := Options{} |
| 117 | vm.options = Options{ |
| 118 | ...previous |
| 119 | max_steps: defaults.max_steps |
| 120 | max_frames: defaults.max_frames |
| 121 | max_expansions: defaults.max_expansions |
| 122 | } |
| 123 | defer { vm.options = previous } |
| 124 | vm.install_goops_metaobjects() |
| 125 | for index in 0 .. goops_protocol_descriptors.len { |
| 126 | entry := &goops_protocol_descriptors[index] |
| 127 | vm.bind_global(entry.name, procedure_value(&Procedure{ |
| 128 | owner: vm.owner |
| 129 | mode: .native |
| 130 | name: entry.name |
| 131 | native: entry.callback |
| 132 | })) |
| 133 | } |
| 134 | vm.bind_global('%goops-unbound', vm.goops.unbound) |
| 135 | context := vm.next_context() |
| 136 | program := goops_protocol_program() |
| 137 | code := &Code{ |
| 138 | ...*program.code |
| 139 | allocation_owner: vm.allocation_owner |
| 140 | syntax_seed: context |
| 141 | builtin_root: false |
| 142 | } |
| 143 | vm.run_program(instantiate_goops_function(program, code), context) or { |
| 144 | panic('cannot bootstrap GOOPS protocols: ${err}') |
| 145 | } |
| 146 | } |
| 147 | |