| 1 | module scheme |
| 2 | |
| 3 | fn (vm &VM) goops_direct_slot(value Value) !GoopsSlot { |
| 4 | if value.tag != .pair { return failure('class slot definition must be a list') } |
| 5 | _ = value.list_length()! |
| 6 | pair := value.pair_pointer() |
| 7 | if pair.car.tag != .symbol { return failure('class slot name must be a symbol') } |
| 8 | mut slot := GoopsSlot{ name: pair.car.text(), init_value: vm.goops.unbound, getter: boolean(false), setter: boolean(false), accessor: boolean(false) } |
| 9 | mut form := Value{ tag: .uninitialized } |
| 10 | mut has_form := false |
| 11 | mut has_allocation := false |
| 12 | mut options := pair.cdr |
| 13 | for options.tag == .pair { |
| 14 | option := options.pair_pointer() |
| 15 | if option.car.tag != .keyword { return failure('class slot option must be a keyword') } |
| 16 | if option.cdr.tag != .pair { return failure('class slot option needs a value') } |
| 17 | argument := option.cdr.pair_pointer() |
| 18 | match option.car.text() { |
| 19 | 'allocation' { |
| 20 | if has_allocation { return failure('duplicate slot allocation option') } |
| 21 | if argument.car.tag != .keyword { |
| 22 | return failure('slot allocation must be a keyword') |
| 23 | } |
| 24 | match argument.car.text() { |
| 25 | 'instance' {} |
| 26 | 'class' { slot.class_allocated = true } |
| 27 | 'virtual' { slot.virtual = true } |
| 28 | 'each-subclass' { |
| 29 | slot.class_allocated = true |
| 30 | slot.each_subclass = true |
| 31 | } |
| 32 | else { return failure('unsupported slot allocation ${argument.car.text()}') } |
| 33 | } |
| 34 | has_allocation = true |
| 35 | } |
| 36 | 'init-keyword' { |
| 37 | if argument.car.tag != .keyword { |
| 38 | return failure('slot init-keyword must be a keyword') |
| 39 | } |
| 40 | if slot.has_init_keyword { return failure('duplicate slot init-keyword option') } |
| 41 | slot.init_keyword = argument.car.text() |
| 42 | slot.has_init_keyword = true |
| 43 | } |
| 44 | 'init-value' { |
| 45 | if slot.has_init_value { return failure('duplicate slot init-value option') } |
| 46 | slot.init_value = argument.car |
| 47 | slot.has_init_value = true |
| 48 | } |
| 49 | 'init-form' { |
| 50 | if has_form { return failure('duplicate slot init-form option') } |
| 51 | form = argument.car |
| 52 | has_form = true |
| 53 | } |
| 54 | 'init-thunk' { |
| 55 | if slot.has_init_thunk { return failure('duplicate slot init-thunk option') } |
| 56 | if !argument.car.is_callable() { |
| 57 | return failure('slot init-thunk must be a procedure') |
| 58 | } |
| 59 | slot.init_thunk = argument.car |
| 60 | slot.has_init_thunk = true |
| 61 | } |
| 62 | 'slot-ref' { |
| 63 | if slot.has_virtual_getter { |
| 64 | return failure('duplicate virtual slot getter') |
| 65 | } |
| 66 | slot.virtual_getter = argument.car |
| 67 | slot.has_virtual_getter = true |
| 68 | } |
| 69 | 'slot-set!' { |
| 70 | if slot.has_virtual_setter { |
| 71 | return failure('duplicate virtual slot setter') |
| 72 | } |
| 73 | slot.virtual_setter = argument.car |
| 74 | slot.has_virtual_setter = true |
| 75 | } |
| 76 | 'getter', 'setter', 'accessor' { |
| 77 | if !argument.car.is_callable() { |
| 78 | return failure('slot ${option.car.text()} must be a generic function') |
| 79 | } |
| 80 | match option.car.text() { |
| 81 | 'getter' { |
| 82 | if slot.getter.is_true() { |
| 83 | return failure('duplicate slot getter') |
| 84 | } |
| 85 | slot.getter = argument.car |
| 86 | } |
| 87 | 'setter' { |
| 88 | if slot.setter.is_true() { |
| 89 | return failure('duplicate slot setter') |
| 90 | } |
| 91 | slot.setter = argument.car |
| 92 | } |
| 93 | else { |
| 94 | if slot.accessor.is_true() { |
| 95 | return failure('duplicate slot accessor') |
| 96 | } |
| 97 | slot.accessor = argument.car |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | else { return failure('unsupported class slot option ${option.car.text()}') } |
| 102 | } |
| 103 | options = argument.cdr |
| 104 | } |
| 105 | if options.tag != .nil { return failure('improper class slot definition') } |
| 106 | mut thunk := if slot.has_init_thunk { slot.init_thunk } else { boolean(false) } |
| 107 | mut options_copy := copy_list(pair.cdr)! |
| 108 | if !slot.has_init_thunk && slot.has_init_value { |
| 109 | thunk = goops_constant_initializer(vm, slot.init_value) |
| 110 | options_copy = cons(keyword('init-thunk')!, cons(thunk, options_copy)) |
| 111 | } |
| 112 | slot.metadata = &GoopsSlotMetadata{ |
| 113 | name: pair.car |
| 114 | options: cons(keyword('name')!, cons(pair.car, options_copy)) |
| 115 | init_form: form |
| 116 | has_init_form: has_form |
| 117 | init_thunk: thunk |
| 118 | allocation: keyword(if slot.virtual { |
| 119 | 'virtual' |
| 120 | } else if slot.each_subclass { |
| 121 | 'each-subclass' |
| 122 | } else if slot.class_allocated { |
| 123 | 'class' |
| 124 | } else { |
| 125 | 'instance' |
| 126 | })! |
| 127 | init_keyword: if slot.has_init_keyword { |
| 128 | keyword(slot.init_keyword)! |
| 129 | } else { |
| 130 | boolean(false) |
| 131 | } |
| 132 | } |
| 133 | return slot |
| 134 | } |
| 135 | |
| 136 | fn goops_slot_index(class &GoopsClass, name string) int { |
| 137 | for index, slot in class.slots { |
| 138 | if slot.name == name { return index } |
| 139 | } |
| 140 | return -1 |
| 141 | } |
| 142 | |
| 143 | fn (mut vm VM) goops_shared_slot_cell(slot GoopsSlot) !&Value { |
| 144 | mut initial := Value{ tag: .uninitialized } |
| 145 | if slot.has_init_thunk { |
| 146 | initial = vm.call(slot.init_thunk, []Value{})! |
| 147 | } else if slot.has_init_value { |
| 148 | initial = slot.init_value |
| 149 | } |
| 150 | if initial.tag == .multiple { return failure('slot initializer requires one value') } |
| 151 | cell := unsafe { &Value(vcalloc(int(sizeof(Value)))) } |
| 152 | unsafe { *cell = initial } |
| 153 | return cell |
| 154 | } |
| 155 | |
| 156 | fn goops_make_class_native(mut vm VM, args []Value) !Value { |
| 157 | check_arity('make-class', args.len, 2, -1)! |
| 158 | if (args.len - 2) % 2 != 0 { return failure('make-class options need keyword/value pairs') } |
| 159 | mut name_value := vm.goops.unbound |
| 160 | for index := 2; index < args.len; index += 2 { |
| 161 | if args[index].tag != .keyword { return failure('make-class option must be a keyword') } |
| 162 | match args[index].text() { |
| 163 | 'name' { |
| 164 | if args[index + 1].tag != .symbol { return failure('class name must be a symbol') } |
| 165 | name_value = args[index + 1] |
| 166 | } |
| 167 | else { return failure('unsupported make-class option ${args[index].text()}') } |
| 168 | } |
| 169 | } |
| 170 | return vm.goops_construct_class(args[0], args[1], name_value, true) |
| 171 | } |
| 172 | |
| 173 | fn (mut vm VM) goops_construct_class(supers_value Value, slots_value Value, supplied_name Value, include_object bool) !Value { |
| 174 | name_value := supplied_name |
| 175 | name := if vm.goops_unbound(name_value) { |
| 176 | '<anonymous>' |
| 177 | } else if name_value.tag == .symbol { |
| 178 | name_value.text() |
| 179 | } else { |
| 180 | write(name_value)! |
| 181 | } |
| 182 | owner := v_arena_current() |
| 183 | mut supers := []&GoopsClass{cap: supers_value.list_length()! + 1} |
| 184 | mut cursor := supers_value |
| 185 | mut has_object := false |
| 186 | for cursor.tag == .pair { |
| 187 | pair := cursor.pair_pointer() |
| 188 | super := vm.goops_retained_class(owner, pair.car)! |
| 189 | for ancestor in super.cpl { |
| 190 | if voidptr(ancestor) == voidptr(vm.goops.object) { has_object = true } |
| 191 | } |
| 192 | supers << super |
| 193 | cursor = pair.cdr |
| 194 | } |
| 195 | if include_object && !has_object { supers << vm.goops.object } |
| 196 | slot_count := slots_value.list_length()! |
| 197 | mut direct := []GoopsSlot{cap: slot_count} |
| 198 | mut definitions := value_storage(slot_count) |
| 199 | cursor = slots_value |
| 200 | for cursor.tag == .pair { |
| 201 | pair := cursor.pair_pointer() |
| 202 | prebuilt := pair.car.tag == .goops_slot |
| 203 | mut slot := if prebuilt { |
| 204 | vm.goops_materialize_slot(pair.car, owner)! |
| 205 | } else if !include_object && pair.car.tag == .symbol { |
| 206 | vm.goops_direct_slot(cons(pair.car, nil_value()))! |
| 207 | } else { |
| 208 | vm.goops_direct_slot(pair.car)! |
| 209 | } |
| 210 | if prebuilt { container_store_validated(mut definitions, direct.len, pair.car) } |
| 211 | if slot.virtual { |
| 212 | if !slot.virtual_getter.is_callable() || !slot.virtual_setter.is_callable() { |
| 213 | return failure('virtual slot needs #:slot-ref and #:slot-set! procedures') |
| 214 | } |
| 215 | vm.check_callable_owner(slot.virtual_getter)! |
| 216 | vm.check_callable_owner(slot.virtual_setter)! |
| 217 | } else { |
| 218 | slot.virtual_getter = Value{} |
| 219 | slot.virtual_setter = Value{} |
| 220 | } |
| 221 | if slot.getter.is_true() { vm.checked_goops_generic(slot.getter)! } |
| 222 | if slot.setter.is_true() { vm.checked_goops_generic(slot.setter)! } |
| 223 | if slot.accessor.is_true() { vm.goops_checked_slot_accessor(slot.accessor)! } |
| 224 | for existing in direct { |
| 225 | if existing.name == slot.name { return failure('duplicate direct slot ${slot.name}') } |
| 226 | } |
| 227 | if slot.class_allocated && !slot.each_subclass { |
| 228 | slot.shared = vm.goops_shared_slot_cell(slot)! |
| 229 | } |
| 230 | direct << slot |
| 231 | cursor = pair.cdr |
| 232 | } |
| 233 | mut class := goops_new_class(vm.owner, name, name_value, supers, vm.goops.meta)! |
| 234 | class.direct_slots = direct |
| 235 | class.direct_definitions = definitions |
| 236 | mut capacity := direct.len |
| 237 | for ancestor in class.cpl[1..] { capacity += ancestor.direct_slots.len } |
| 238 | mut slots := []GoopsSlot{cap: capacity} |
| 239 | // Guile lays out inherited slots from the least specific class first. |
| 240 | // A more specific definition replaces metadata at that slot's index. |
| 241 | for ancestor_index := class.cpl.len - 1; ancestor_index >= 0; ancestor_index-- { |
| 242 | ancestor := class.cpl[ancestor_index] |
| 243 | for slot in ancestor.direct_slots { |
| 244 | mut found_index := -1 |
| 245 | for index, previous in slots { |
| 246 | if previous.name == slot.name { |
| 247 | found_index = index |
| 248 | break |
| 249 | } |
| 250 | } |
| 251 | if found_index >= 0 { |
| 252 | slots[found_index] = slot |
| 253 | } else { |
| 254 | slots << slot |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | class.slots = slots |
| 259 | for index, slot in slots { |
| 260 | if slot.each_subclass { |
| 261 | // Every class, including the defining class, owns one cell. Direct |
| 262 | // metadata keeps no cell so a later subclass cannot inherit this one. |
| 263 | class.slots[index].shared = vm.goops_shared_slot_cell(slot)! |
| 264 | } |
| 265 | if !slot.class_allocated && !slot.virtual { |
| 266 | class.slots[index].instance_index = class.instance_fields |
| 267 | class.instance_fields++ |
| 268 | } |
| 269 | if (slot.virtual && (slot.has_init_keyword || slot.has_init_value || slot.has_init_thunk)) |
| 270 | || (!slot.class_allocated && slot.has_init_thunk) { |
| 271 | class.has_init_thunks = true |
| 272 | } |
| 273 | } |
| 274 | for ancestor in class.cpl { |
| 275 | if voidptr(ancestor) == voidptr(vm.goops.applicable_struct) { |
| 276 | class.call_slot = goops_slot_index(class, 'procedure') |
| 277 | if class.call_slot < 0 { return failure('applicable class has no procedure slot') } |
| 278 | break |
| 279 | } |
| 280 | } |
| 281 | for slot in direct { |
| 282 | if slot.getter.is_true() { |
| 283 | vm.goops_add_slot_method(slot.getter, class, slot.metadata.name, false)! |
| 284 | } |
| 285 | if slot.setter.is_true() { |
| 286 | vm.goops_add_slot_method(slot.setter, class, slot.metadata.name, true)! |
| 287 | } |
| 288 | if slot.accessor.is_true() { |
| 289 | getter, setter := vm.goops_checked_slot_accessor(slot.accessor)! |
| 290 | vm.goops_add_slot_method(if slot.accessor.procedure_pointer().mode in [ |
| 291 | .goops_accessor, |
| 292 | .goops_generic_setter, |
| 293 | ] { |
| 294 | slot.accessor |
| 295 | } else { |
| 296 | getter |
| 297 | }, class, slot.metadata.name, false)! |
| 298 | vm.goops_add_slot_method(setter, class, slot.metadata.name, true)! |
| 299 | } |
| 300 | } |
| 301 | vm.install_goops_slot_definitions(mut class) |
| 302 | return goops_class_value(class) |
| 303 | } |
| 304 | |
| 305 | @[inline] |
| 306 | fn goops_field_offset() int { |
| 307 | width := int(sizeof(Value)) |
| 308 | return ((int(sizeof(GoopsInstance)) + width - 1) / width) * width |
| 309 | } |
| 310 | |
| 311 | fn (vm &VM) checked_goops_instance(value Value) !&GoopsInstance { |
| 312 | if value.goops_kind() != .goops_instance { return failure('expected a GOOPS instance') } |
| 313 | instance := value.goops_instance_pointer() |
| 314 | if voidptr(instance.class.owner) != voidptr(vm.owner) { |
| 315 | return failure('instance belongs to another VM') |
| 316 | } |
| 317 | return instance |
| 318 | } |
| 319 | |
| 320 | @[inline] |
| 321 | fn (mut vm VM) goops_instance_target(value Value) !Value { |
| 322 | instance := vm.checked_goops_instance(value)! |
| 323 | index := instance.class.call_slot |
| 324 | if index < 0 { return failure('attempt to call a goops_instance') } |
| 325 | slot := &instance.class.slots[index] |
| 326 | target := if slot.virtual { |
| 327 | vm.goops_slot_read(value, slot.name)! |
| 328 | } else if slot.class_allocated { |
| 329 | unsafe { *slot.shared } |
| 330 | } else if slot.native_field != .none { |
| 331 | vm.goops_native_slot_read(value, slot.native_field)! |
| 332 | } else { |
| 333 | unsafe { instance.fields[slot.instance_index] } |
| 334 | } |
| 335 | if vm.goops_unbound(target) { |
| 336 | return failure('applicable instance has an unbound procedure slot') |
| 337 | } |
| 338 | return target |
| 339 | } |
| 340 | |
| 341 | @[inline] |
| 342 | fn goops_call_redirects(value Value) bool { |
| 343 | return value.goops_kind() == .goops_instance || (value.tag == .procedure |
| 344 | && value.procedure_pointer().mode in [.with_setter, .goops_generic_setter, .goops_accessor]) |
| 345 | } |
| 346 | |
| 347 | fn (mut vm VM) goops_call_redirect(value Value) !Value { |
| 348 | if value.goops_kind() == .goops_instance { return vm.goops_instance_target(value) } |
| 349 | vm.check_callable_owner(value)! |
| 350 | return value.procedure_pointer().env.slots[0] |
| 351 | } |
| 352 | |
| 353 | // Follow applicable instances and their procedure-with-setter wrappers without |
| 354 | // adding an interpreter frame or an argument copy. Floyd's walk rejects cycles |
| 355 | // through either kind of forwarding object. |
| 356 | fn (mut vm VM) goops_resolve_callable(value Value) !Value { |
| 357 | mut slow := value |
| 358 | mut fast := value |
| 359 | for goops_call_redirects(slow) { |
| 360 | slow = vm.goops_call_redirect(slow)! |
| 361 | for _ in 0 .. 2 { |
| 362 | if !goops_call_redirects(fast) { break } |
| 363 | fast = vm.goops_call_redirect(fast)! |
| 364 | } |
| 365 | if goops_call_redirects(slow) && goops_call_redirects(fast) |
| 366 | && slow.tag == fast.tag && slow.pointer() == fast.pointer() { |
| 367 | return failure('cyclic applicable instance') |
| 368 | } |
| 369 | } |
| 370 | return slow |
| 371 | } |
| 372 | |
| 373 | fn goops_make_native(mut vm VM, args []Value) !Value { |
| 374 | check_arity('make', args.len, 1, -1)! |
| 375 | class := vm.checked_goops_class(args[0])! |
| 376 | if !vm.goops_retains_owner(v_arena_current(), class.allocation_owner) { |
| 377 | return failure('instance class crosses an arena boundary') |
| 378 | } |
| 379 | if voidptr(class) == voidptr(vm.goops.generic_class) { |
| 380 | return vm.goops_make_generic(args) |
| 381 | } |
| 382 | if voidptr(class) == voidptr(vm.goops.generic_setter_class) || voidptr(class) == voidptr(vm.goops.accessor_class) { |
| 383 | return vm.goops_make_generic_with_setter(args, voidptr(class) == voidptr(vm.goops.accessor_class)) |
| 384 | } |
| 385 | if voidptr(class) == voidptr(vm.goops.method_class) { |
| 386 | return vm.goops_make_method(args) |
| 387 | } |
| 388 | if voidptr(class) == voidptr(vm.goops.slot_class) { |
| 389 | return vm.goops_make_slot(args) |
| 390 | } |
| 391 | if voidptr(class) == voidptr(vm.goops.meta) { |
| 392 | return goops_make_class_object(mut vm, args) |
| 393 | } |
| 394 | mut constructible := false |
| 395 | for ancestor in class.cpl { |
| 396 | if voidptr(ancestor) == voidptr(vm.goops.object) { |
| 397 | constructible = true |
| 398 | break |
| 399 | } |
| 400 | } |
| 401 | if !constructible { return failure('cannot make an instance of builtin non-object class') } |
| 402 | if (args.len - 1) % 2 != 0 { return failure('make initargs need keyword/value pairs') } |
| 403 | if class.instance_fields > (max_int - goops_field_offset()) / int(sizeof(Value)) { |
| 404 | return failure('class has too many instance slots') |
| 405 | } |
| 406 | for index := 1; index < args.len; index += 2 { |
| 407 | if args[index].tag != .keyword { return failure('make initarg must be a keyword') } |
| 408 | } |
| 409 | offset := goops_field_offset() |
| 410 | data := unsafe { vcalloc(offset + class.instance_fields * int(sizeof(Value))) } |
| 411 | fields := if class.instance_fields == 0 { |
| 412 | unsafe { &Value(nil) } |
| 413 | } else { |
| 414 | unsafe { &Value(data + offset) } |
| 415 | } |
| 416 | mut instance := unsafe { &GoopsInstance(data) } |
| 417 | unsafe { *instance = GoopsInstance{ class: class, fields: fields, field_count: class.instance_fields, layout_version: class.version } } |
| 418 | object := payload_value(.goops_instance, instance) |
| 419 | if class.has_init_thunks { |
| 420 | for slot_index in 0 .. class.slots.len { |
| 421 | slot := &class.slots[slot_index] |
| 422 | if slot.class_allocated { |
| 423 | if slot.has_init_keyword { |
| 424 | for index := 1; index < args.len; index += 2 { |
| 425 | if args[index].text() == slot.init_keyword { |
| 426 | unsafe { *slot.shared = args[index + 1] } |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | continue |
| 431 | } |
| 432 | mut supplied := false |
| 433 | mut value := Value{ tag: .uninitialized } |
| 434 | if slot.has_init_keyword { |
| 435 | for index := 1; index < args.len; index += 2 { |
| 436 | if args[index].text() == slot.init_keyword { |
| 437 | value = args[index + 1] |
| 438 | supplied = true |
| 439 | } |
| 440 | } |
| 441 | } |
| 442 | if !supplied { |
| 443 | if slot.has_init_thunk { |
| 444 | value = vm.call(slot.init_thunk, []Value{})! |
| 445 | } else if slot.has_init_value { |
| 446 | value = slot.init_value |
| 447 | } |
| 448 | } |
| 449 | if value.tag == .multiple { return failure('slot initializer requires one value') } |
| 450 | if slot.virtual { |
| 451 | if value.tag != .uninitialized { vm.goops_slot_write(object, slot.name, value)! } |
| 452 | } else { |
| 453 | unsafe { fields[slot.instance_index] = value } |
| 454 | } |
| 455 | } |
| 456 | } else { |
| 457 | for slot_index in 0 .. class.slots.len { |
| 458 | slot := &class.slots[slot_index] |
| 459 | if slot.class_allocated || slot.virtual { continue } |
| 460 | unsafe { |
| 461 | fields[slot.instance_index] = if slot.has_init_value { |
| 462 | slot.init_value |
| 463 | } else { |
| 464 | Value{ tag: .uninitialized } |
| 465 | } |
| 466 | } |
| 467 | } |
| 468 | for index := 1; index < args.len; index += 2 { |
| 469 | key := args[index].text() |
| 470 | for slot_index in 0 .. class.slots.len { |
| 471 | slot := &class.slots[slot_index] |
| 472 | if slot.has_init_keyword && slot.init_keyword == key { |
| 473 | if slot.class_allocated { |
| 474 | unsafe { *slot.shared = args[index + 1] } |
| 475 | } else { |
| 476 | unsafe { fields[slot.instance_index] = args[index + 1] } |
| 477 | } |
| 478 | break |
| 479 | } |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 | return vm.register_goops_finalizer(class, object) |
| 484 | } |
| 485 | |
| 486 | fn goops_slot_request(mut vm VM, args []Value, operation string) !(&GoopsInstance, int) { |
| 487 | if args[0].goops_kind() != .goops_instance { |
| 488 | if args[0].is_callable() { vm.check_callable_owner(args[0])! } |
| 489 | vm.goops_class_for(args[0])! |
| 490 | if args[1].tag != .symbol { return failure('${operation} needs a symbol slot name') } |
| 491 | return unsafe { &GoopsInstance(nil) }, -1 |
| 492 | } |
| 493 | instance := vm.checked_goops_instance(args[0])! |
| 494 | if args[1].tag != .symbol { return failure('${operation} needs a symbol slot name') } |
| 495 | index := goops_slot_index(instance.class, args[1].text()) |
| 496 | return instance, index |
| 497 | } |
| 498 | |
| 499 | fn (vm &VM) goops_slot_definition(object Value, name string) !&GoopsSlot { |
| 500 | class := vm.goops_class_for(object)! |
| 501 | index := goops_slot_index(class, name) |
| 502 | if index < 0 { return goops_missing_slot_error } |
| 503 | return &class.slots[index] |
| 504 | } |
| 505 | |
| 506 | fn (vm &VM) goops_class_slot_definition(class_value Value, name_value Value, operation string) !&GoopsSlot { |
| 507 | class := vm.checked_goops_class(class_value)! |
| 508 | if name_value.tag != .symbol { return failure('${operation} needs a symbol slot name') } |
| 509 | name := name_value.text() |
| 510 | index := goops_slot_index(class, name) |
| 511 | if index < 0 || !class.slots[index].class_allocated { |
| 512 | return goops_missing_slot_error |
| 513 | } |
| 514 | return &class.slots[index] |
| 515 | } |
| 516 | |
| 517 | fn goops_class_slot_ref_native(mut vm VM, args []Value) !Value { |
| 518 | check_arity('class-slot-ref', args.len, 2, 2)! |
| 519 | slot := vm.goops_class_slot_definition(args[0], args[1], 'class-slot-ref')! |
| 520 | value := unsafe { *slot.shared } |
| 521 | if vm.goops_unbound(value) { return goops_unbound_slot_error } |
| 522 | return value |
| 523 | } |
| 524 | |
| 525 | fn goops_class_slot_set_native(mut vm VM, args []Value) !Value { |
| 526 | check_arity('class-slot-set!', args.len, 3, 3)! |
| 527 | slot := vm.goops_class_slot_definition(args[0], args[1], 'class-slot-set!')! |
| 528 | unsafe { *slot.shared = args[2] } |
| 529 | return unspecified() |
| 530 | } |
| 531 | |
| 532 | fn (mut vm VM) goops_slot_read(object Value, name string) !Value { |
| 533 | slot := vm.goops_slot_definition(object, name)! |
| 534 | return vm.goops_slot_read_resolved(object, slot) |
| 535 | } |
| 536 | |
| 537 | fn (mut vm VM) goops_slot_read_resolved(object Value, slot &GoopsSlot) !Value { |
| 538 | value := if slot.virtual { |
| 539 | vm.call_small(slot.virtual_getter, object, Value{}, 1)! |
| 540 | } else if slot.class_allocated { |
| 541 | unsafe { *slot.shared } |
| 542 | } else if slot.native_field != .none { |
| 543 | vm.goops_native_slot_read(object, slot.native_field)! |
| 544 | } else { |
| 545 | unsafe { *vm.goops_field_pointer(object, slot.instance_index)! } |
| 546 | } |
| 547 | if vm.goops_unbound(value) { return goops_unbound_slot_error } |
| 548 | return value |
| 549 | } |
| 550 | |
| 551 | fn (mut vm VM) goops_slot_write(object Value, name string, value Value) !Value { |
| 552 | slot := vm.goops_slot_definition(object, name)! |
| 553 | return vm.goops_slot_write_resolved(object, slot, value) |
| 554 | } |
| 555 | |
| 556 | fn (mut vm VM) goops_slot_write_resolved(object Value, slot &GoopsSlot, value Value) !Value { |
| 557 | goops_check_slot_value(value)! |
| 558 | return vm.goops_slot_write_validated(object, slot, value) |
| 559 | } |
| 560 | |
| 561 | @[inline] |
| 562 | fn goops_check_slot_value(value Value) ! { |
| 563 | if value.tag == .multiple { return failure('slot value requires one value') } |
| 564 | } |
| 565 | |
| 566 | // Callers validate the value before invoking a virtual callback or changing a |
| 567 | // shared cell. Internal dispatch can then reuse this store without rechecking. |
| 568 | fn (mut vm VM) goops_slot_write_validated(object Value, slot &GoopsSlot, value Value) !Value { |
| 569 | if slot.virtual { |
| 570 | return vm.call_small(slot.virtual_setter, object, value, 2) |
| 571 | } else if slot.class_allocated { |
| 572 | unsafe { *slot.shared = value } |
| 573 | } else if slot.native_field != .none { |
| 574 | return vm.goops_native_slot_write(object, slot.native_field, value, true) |
| 575 | } else { |
| 576 | field := vm.goops_field_pointer(object, slot.instance_index)! |
| 577 | unsafe { *field = value } |
| 578 | } |
| 579 | return value |
| 580 | } |
| 581 | |
| 582 | fn goops_slot_bound_native(mut vm VM, args []Value) !Value { |
| 583 | check_arity('slot-bound?', args.len, 2, 2)! |
| 584 | instance, index := goops_slot_request(mut vm, args, 'slot-bound?')! |
| 585 | if index < 0 { return failure('missing slot ${args[1].text()}') } |
| 586 | slot := &instance.class.slots[index] |
| 587 | if slot.virtual { |
| 588 | return boolean(!vm.goops_unbound(vm.call_small(slot.virtual_getter, args[0], Value{}, 1)!)) |
| 589 | } |
| 590 | return boolean(if slot.class_allocated { |
| 591 | !vm.goops_unbound(unsafe { *slot.shared }) |
| 592 | } else { |
| 593 | !vm.goops_unbound(unsafe { instance.fields[slot.instance_index] }) |
| 594 | }) |
| 595 | } |
| 596 | |
| 597 | fn goops_slot_exists_native(mut vm VM, args []Value) !Value { |
| 598 | check_arity('slot-exists?', args.len, 2, 2)! |
| 599 | class := vm.goops_class_for(args[0])! |
| 600 | if args[1].tag != .symbol { return failure('slot-exists? needs a symbol slot name') } |
| 601 | return boolean(goops_slot_index(class, args[1].text()) >= 0) |
| 602 | } |
| 603 | |