GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — livecode/libscript/src/script-execute.cpp at develop · livecode/livecode
This repository was archived by the owner on Aug 31, 2021. It is now read-only.
Expand file tree/ Copy pathscript-execute.cppMore file actions 1256 lines (1110 loc) · 38 KB / Copy pathscript-execute.cppFile metadata and controls1256 lines (1110 loc) · 38 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 /* Copyright (C) 2003-2016 LiveCode Ltd. This file is part of LiveCode. LiveCode is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License v3 as published by the Free Software Foundation. LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with LiveCode. If not see <http://www.gnu.org/licenses/>. */ #include "libscript/script.h" #include "script-private.h" #include "ffi.h" #include "foundation-auto.h" #ifdef _WIN32 #include <windows.h> #else #include <dlfcn.h> #endif #if defined(_MACOSX) || defined(TARGET_SUBPLATFORM_IPHONE) #include <objc/message.h> #endif #include "script-execute.hpp" typedef void (*__MCScriptValueDrop)(void *); static void __MCScriptDropValueRef(void *p_value) { MCValueRelease(*(MCValueRef *)p_value); } class MCScriptForeignInvocation { public: struct CallTrampolineContext { MCScriptForeignInvocation* invocation; MCScriptForeignHandlerDefinition *handler; MCTypeInfoRef signature; void *result_slot_ptr; bool return_value; }; /**/ MCScriptForeignInvocation(void) : m_argument_count(0), m_storage_frontier(0) { } ~MCScriptForeignInvocation(void) { // Drop any slots which have a drop method (those whose // value have been taken will have had their drop reset // to nil). for(uindex_t i = 0; i < m_argument_count; i++) { if (m_argument_drops[i] != nil) { m_argument_drops[i](m_argument_slots[i]); } } } // Allocate temporary storage for the invocation bool Allocate(size_t p_amount, size_t p_align, void*& r_ptr) { // Compute any padding needed for alignment (t_align_delta will // be the number of pad bytes). size_t t_align_delta; t_align_delta = p_align - (m_storage_frontier % p_align); // If there is not enough space then throw. if (sizeof(m_stack_storage) - m_storage_frontier < p_amount + t_align_delta) { return MCErrorThrowOutOfMemory(); } // Round the frontier up to the required alignment. m_storage_frontier += t_align_delta; // Compute the returned pointer into the actual storage array r_ptr = m_stack_storage + m_storage_frontier; // Bump the frontier by the amount allocated. m_storage_frontier += p_amount; return true; } // Append a non-reference argument - this takes contents_ptr bool Argument(void *p_slot_ptr, __MCScriptValueDrop p_slot_drop, ffi_type* p_type) { if (m_argument_count >= kMaxArguments) { return MCErrorThrowOutOfMemory(); } // For normal arguments, the slot is passed directly. m_argument_values[m_argument_count] = p_slot_ptr; // Store the slot ptr and its drop. m_argument_slots[m_argument_count] = p_slot_ptr; m_argument_drops[m_argument_count] = p_slot_drop; // Store the ffi_type. m_argument_types[m_argument_count] = p_type; // Bump the number of arguments m_argument_count++; return true; } // Append a reference argument - this takes contents_ptr bool ReferenceArgument(void *p_slot_ptr, __MCScriptValueDrop p_slot_drop) { if (m_argument_count >= kMaxArguments || !Allocate(sizeof(void *), alignof(void *), m_argument_values[m_argument_count])) { return MCErrorThrowOutOfMemory(); } // Store the slot into the reference slot we just created. *(void **)m_argument_values[m_argument_count] = p_slot_ptr; // Store the slot ptr and its drop. m_argument_slots[m_argument_count] = p_slot_ptr; m_argument_drops[m_argument_count] = p_slot_drop; // Reference arguments always have type ffi_pointer. m_argument_types[m_argument_count] = &ffi_type_pointer; // Bump the number of arguments m_argument_count++; return true; } /**/ bool CallJava(MCScriptForeignHandlerDefinition *p_handler, MCTypeInfoRef p_handler_signature, void *p_result_slot_ptr) { if (p_handler->thread_affinity != kMCScriptThreadAffinityDefault) { #if defined(TARGET_SUBPLATFORM_ANDROID) && !defined(CROSS_COMPILE_HOST) extern void *MCAndroidGetSystemJavaEnv(); extern bool MCJavaPrivateCallJNIMethodOnEnv(void *p_env, MCNameRef p_class, void *p_method_id, int p_call_type, MCTypeInfoRef p_signature, void *r_return, void **p_args, uindex_t p_arg_count); return MCJavaPrivateCallJNIMethodOnEnv(MCAndroidGetSystemJavaEnv(), p_handler->java.class_name, p_handler->java.method_id, p_handler->java.call_type, p_handler_signature, p_result_slot_ptr, m_argument_values, m_argument_count); #endif } return MCJavaCallJNIMethod(p_handler->java.class_name, p_handler->java.method_id, p_handler->java.call_type, p_handler_signature, p_result_slot_ptr, m_argument_values, m_argument_count); } /**/ bool CallC(MCScriptForeignHandlerDefinition *p_handler, MCTypeInfoRef p_handler_signature, void *p_result_slot_ptr) { MCAssert(p_handler->language == kMCScriptForeignHandlerLanguageC); if (!MCHandlerTypeInfoIsVariadic(p_handler_signature)) { ffi_call((ffi_cif *)p_handler ->c.function_cif, (void(*)())p_handler ->c.function, p_result_slot_ptr, m_argument_values); } else { ffi_cif *t_fixed_cif = (ffi_cif *)p_handler->c.function_cif; ffi_cif t_cif; if (ffi_prep_cif_var(&t_cif, t_fixed_cif->abi, t_fixed_cif->nargs, m_argument_count, t_fixed_cif->rtype, m_argument_types) != FFI_OK) { return MCErrorThrowGeneric(MCSTR("unexpected libffi failure")); } ffi_call(&t_cif, (void(*)())p_handler ->c.function, p_result_slot_ptr, m_argument_values); } return true; } /**/ bool CallObjC(MCScriptForeignHandlerDefinition *p_handler, MCTypeInfoRef p_handler_signature, void *p_result_slot_ptr) { #if defined(_MACOSX) || defined(TARGET_SUBPLATFORM_IPHONE) /* At this point we have a argument values array which will match * the LCB signature - depending on the call type we need to modify * it. */ void *t_objc_values[kMaxArguments]; if (p_handler->objc.call_type == kMCScriptForeignHandlerObjcCallTypeInstanceMethod) { /* In the case of an instance method call, we need to insert * the method pointer as the second argument. */ if (m_argument_count + 1 >= kMaxArguments) { return MCErrorThrowOutOfMemory(); } t_objc_values[0] = m_argument_values[0]; t_objc_values[1] = &p_handler->objc.objc_selector; for(uindex_t i = 1; i < m_argument_count; i++) t_objc_values[i + 1] = m_argument_values[i]; } else if (p_handler->objc.call_type == kMCScriptForeignHandlerObjcCallTypeClassMethod) { /* In the case of a class method call, we need to insert both * the class, and method pointer as the first two arguments. */ if (m_argument_count + 2 >= kMaxArguments) { return MCErrorThrowOutOfMemory(); } t_objc_values[0] = &p_handler->objc.objc_class; t_objc_values[1] = &p_handler->objc.objc_selector; for(uindex_t i = 0; i < m_argument_count; i++) t_objc_values[i + 2] = m_argument_values[i]; } /* There are different variants of objc_msgSend we need to use * depending on architecture and return type: * struct return * arm64: only has objc_msgSend * arm: _stret if struct size > 4 bytes * x86-64: _stret if struct size > 16 bytes * i386: _stret if struct size >= 8 bytes or * exactly 1, 2, or 4 bytes * float return * arm: no difference * i386: _fpret * x86-64: no difference * double return * arm: no difference * i386: _fpret * x86-64: no difference * long double * arm: no difference * i386: _fpret * x86-64: _fpret * _Complex long double (TODO) * arm: no difference * i386: no difference * x86-64: _fp2ret */ ffi_cif *t_cif = (ffi_cif *)p_handler->objc.function_cif; void (*t_objc_msgSend)() = nullptr; size_t t_rsize = t_cif->rtype->size; #if defined(__ARM64__) t_objc_msgSend = (void(*)())objc_msgSend; #elif defined(__ARM__) if (t_cif->rtype->type == FFI_TYPE_STRUCT && t_rsize > 4) t_objc_msgSend = (void(*)())objc_msgSend_stret; else t_objc_msgSend = (void(*)())objc_msgSend; #elif defined(__X86_64__) /* TODO: Investigate structs containing long doubles * as they will not work at the moment */ if (t_cif->rtype->type == FFI_TYPE_STRUCT && t_rsize > 16) t_objc_msgSend = (void(*)())objc_msgSend_stret; else if (t_cif->rtype->type == FFI_TYPE_LONGDOUBLE) t_objc_msgSend = (void(*)())objc_msgSend_fpret; else t_objc_msgSend = (void(*)())objc_msgSend; #elif defined(__i386__) if (t_cif->rtype->type == FFI_TYPE_STRUCT && (t_rsize >= 8 || (t_rsize & (t_rsize - 1)) == 0)) t_objc_msgSend = (void(*)())objc_msgSend_stret; else if (t_cif->rtype->type == FFI_TYPE_FLOAT || t_cif->rtype->type == FFI_TYPE_DOUBLE || t_cif->rtype->type == FFI_TYPE_LONGDOUBLE) t_objc_msgSend = (void(*)())objc_msgSend_fpret; else t_objc_msgSend = (void(*)())objc_msgSend; #else #error unknown architecture for objc_msgSend #endif /* We must use a wrapper function written in an obj-c++ source file so that * we can capture any obj-c exceptions. */ extern bool MCScriptCallObjCCatchingErrors(ffi_cif*, void (*)(), void *, void **); return MCScriptCallObjCCatchingErrors(t_cif, t_objc_msgSend, p_result_slot_ptr, t_objc_values); #else return true; #endif } bool CallAny(MCScriptForeignHandlerDefinition *p_handler, MCTypeInfoRef p_handler_signature, void *p_result_slot_ptr) { switch(p_handler->language) { case kMCScriptForeignHandlerLanguageJava: return CallJava(p_handler, p_handler_signature, p_result_slot_ptr); case kMCScriptForeignHandlerLanguageC: return CallC(p_handler, p_handler_signature, p_result_slot_ptr); case kMCScriptForeignHandlerLanguageBuiltinC: ((void(*)(void*, void**))p_handler->builtin_c.function)(p_result_slot_ptr, m_argument_values); return true; case kMCScriptForeignHandlerLanguageObjC: return CallObjC(p_handler, p_handler_signature, p_result_slot_ptr); case kMCScriptForeignHandlerLanguageUnknown: MCUnreachableReturn(false); } return false; } static void CallAnyTrampoline(void *p_context) { auto ctxt = static_cast<CallTrampolineContext *>(p_context); ctxt->return_value = ctxt->invocation->CallAny(ctxt->handler, ctxt->signature, ctxt->result_slot_ptr); } // Call the foreign handler, taking into account which thread it must run // on. bool Call(MCScriptForeignHandlerDefinition *p_handler, MCTypeInfoRef p_handler_signature, void *p_result_slot_ptr) { /* If the handler has a thread affinity which is non-default, then on split * thread engines (android / ios) we must do the actual call on the other * thread. */ if (p_handler->thread_affinity != kMCScriptThreadAffinityDefault) { #if defined(TARGET_SUBPLATFORM_IPHONE) && !defined(CROSS_COMPILE_HOST) extern void MCIPhoneRunOnMainFiber(void (*)(void *), void *); CallTrampolineContext t_context = { this, p_handler, p_handler_signature, p_result_slot_ptr, true }; MCIPhoneRunOnMainFiber(CallAnyTrampoline, &t_context); return t_context.return_value; #elif defined(TARGET_SUBPLATFORM_ANDROID) && !defined(CROSS_COMPILE_HOST) typedef void (*co_yield_callback_t)(void *); extern void co_yield_to_android_and_call(co_yield_callback_t callback, void *context); CallTrampolineContext t_context = { this, p_handler, p_handler_signature, p_result_slot_ptr, true }; co_yield_to_android_and_call(CallAnyTrampoline, &t_context); return t_context.return_value; #endif } /* Either thread affinity is default, or the platform does not have split * threads. */ return CallAny(p_handler, p_handler_signature, p_result_slot_ptr); } // Take the contents of a slot - this stops the invocation // cleaning up the taken slot. void *TakeArgument(uindex_t p_index) { void *t_slot_ptr; t_slot_ptr = m_argument_slots[p_index]; m_argument_slots[p_index] = nil; m_argument_drops[p_index] = nil; return t_slot_ptr; } private: enum { kMaxArguments = 32, kMaxStorage = 4096, }; // The number of arguments currently accumulated. uindex_t m_argument_count; // The array of pointers which will be passed to libffi - this will // be the pointers in m_argument_slots for normal parameters and // a pointer to the pointers in m_argument_slots for reference // parameters. void *m_argument_values[kMaxArguments]; // The drops for the slot values in m_argument_slots. __MCScriptValueDrop m_argument_drops[kMaxArguments]; // The actual values in the slots (not indirected for references). void *m_argument_slots[kMaxArguments]; // The ffi_types of the arguments (used for var-args) ffi_type *m_argument_types[kMaxArguments]; size_t m_storage_frontier; char m_stack_storage[kMaxStorage]; }; inline void __MCScriptComputeSlotAttributes(MCTypeInfoRef p_slot_type, size_t& r_slot_size, size_t& r_slot_align, __MCScriptValueDrop& r_slot_drop) { // If the slot is foreign then we interrogate the foreign descriptor. if (MCTypeInfoIsForeign(p_slot_type)) { const MCForeignTypeDescriptor *t_desc = MCForeignTypeInfoGetDescriptor(p_slot_type); r_slot_size = t_desc->size; r_slot_align = t_desc->size; r_slot_drop = t_desc->finalize; return; } // If the slot is a foreign handler then it is pointer sized. if (MCTypeInfoIsHandler(p_slot_type) && MCHandlerTypeInfoIsForeign(p_slot_type)) { r_slot_size = sizeof(void*); r_slot_align = alignof(void*); r_slot_drop = nil; return; } r_slot_size = sizeof(void*); r_slot_align = alignof(void*); r_slot_drop = __MCScriptDropValueRef; } bool MCScriptExecuteContext::InvokeForeignVarArgument(MCScriptForeignInvocation& p_invocation, MCScriptInstanceRef p_instance, MCScriptForeignHandlerDefinition *p_handler_def, uindex_t p_arg_index, uindex_t p_arg_reg) { // Get the value in the register, this determines the type. MCValueRef t_arg_value = CheckedFetchRegister(p_arg_reg); if (t_arg_value == nil) { return false; } // Resolve the type of the value. MCResolvedTypeInfo t_resolved_type; if (!ResolveTypeInfo(MCValueGetTypeInfo(t_arg_value), t_resolved_type)) { return false; } // Store the actual type of the value. MCTypeInfoRef t_actual_type = t_resolved_type.type; // Fetch the promoted type of arg type (sub ints promote to int, and float // promotes to double) - but only if the actual type is foreign. const MCForeignTypeDescriptor *t_desc = nullptr; if (MCTypeInfoIsForeign(t_actual_type)) { t_desc = MCForeignTypeInfoGetDescriptor(t_actual_type); } MCTypeInfoRef t_arg_type = t_actual_type; if (t_desc != nullptr) { if (t_desc->promote != nullptr) { MCResolvedTypeInfo t_resolved_arg_type; if (!ResolveTypeInfo(t_desc->promotedtype, t_resolved_arg_type)) { return false; } t_arg_type = t_resolved_arg_type.type; } } // Compute the slot attributes - using the (potentially) promoted type. size_t t_slot_size = 0; size_t t_slot_align = 0; __MCScriptValueDrop t_slot_drop = nil; __MCScriptComputeSlotAttributes(t_arg_type, t_slot_size, t_slot_align, t_slot_drop); // Allocate room for the slot in the invocation. void *t_slot_ptr = nil; if (!p_invocation.Allocate(t_slot_size, t_slot_align, t_slot_ptr)) { Rethrow(); return false; } // Convert the value to an unboxed one - we use the actual type of the argument // here. // TODO: Split UnboxingConvert so that we don't resolve types twice. if (!UnboxingConvert(t_arg_value, t_resolved_type, t_slot_ptr)) { return false; } // If we don't get a slot out, then it failed. if (t_slot_ptr == nil) { ThrowInvalidValueForArgument(p_instance, p_handler_def, p_arg_index, t_arg_value); return false; } // If we must promote the type, do it next. Note that the slot ptr is big // enough to hold the promoted value, and we do this 'in place. if (t_arg_type != t_actual_type) { t_desc->promote(t_slot_ptr); } // Fetch the ffi_type to use - notice that we must use the *promoted* type // here. ffi_type *t_type = nullptr; if (t_desc != nullptr) { t_type = (ffi_type *)MCForeignTypeInfoGetLayoutType(t_arg_type); } else { t_type = &ffi_type_pointer; } if (!p_invocation.Argument(t_slot_ptr, t_slot_drop, t_type)) { Rethrow(); return false; } return true; } bool MCScriptExecuteContext::InvokeForeignArgument(MCScriptForeignInvocation& p_invocation, MCScriptInstanceRef p_instance, MCScriptForeignHandlerDefinition *p_handler_def, uindex_t p_arg_index, MCHandlerTypeFieldMode p_mode, MCTypeInfoRef p_param_type, uindex_t p_arg_reg) { // Compute the parameter's slot size and allocate storage for it MCResolvedTypeInfo t_resolved_param_type; if (!ResolveTypeInfo(p_param_type, t_resolved_param_type)) { return false; } size_t t_slot_size = 0; size_t t_slot_align = 0; __MCScriptValueDrop t_slot_drop = nil; __MCScriptComputeSlotAttributes(t_resolved_param_type.type, t_slot_size, t_slot_align, t_slot_drop); void *t_slot_ptr = nil; if (!p_invocation.Allocate(t_slot_size, t_slot_align, t_slot_ptr)) { Rethrow(); return false; } // If the mode is not out, then we have an initial value to initialize // it with; otherwise we must initialize it directly. MCValueRef t_arg_value = nil; if (p_mode != kMCHandlerTypeFieldModeOut) { t_arg_value = CheckedFetchRegister(p_arg_reg); if (t_arg_value == nil) { return false; } } // Convert the value to an unboxed one. if (!UnboxingConvert(t_arg_value, t_resolved_param_type, t_slot_ptr)) { return false; } if (t_slot_ptr == nil) { ThrowInvalidValueForArgument(p_instance, p_handler_def, p_arg_index, t_arg_value); return false; } if (p_mode == kMCHandlerTypeFieldModeIn) { ffi_type *t_type = nullptr; if (MCTypeInfoIsForeign(t_resolved_param_type.type)) { t_type = (ffi_type *)MCForeignTypeInfoGetLayoutType(t_resolved_param_type.type); } else { t_type = &ffi_type_pointer; } if (!p_invocation.Argument(t_slot_ptr, t_slot_drop, t_type)) { Rethrow(); return false; } } else { if (!p_invocation.ReferenceArgument(t_slot_ptr, t_slot_drop)) { Rethrow(); return false; } } return true; } void MCScriptExecuteContext::InvokeForeign(MCScriptInstanceRef p_instance, MCScriptForeignHandlerDefinition *p_handler_def, uindex_t p_result_reg, MCSpan<const uindex_t> p_argument_regs) { if (m_error) { return; } if (p_handler_def->language == kMCScriptForeignHandlerLanguageUnknown) { if (!MCScriptBindForeignHandlerInInstanceInternal(p_instance, p_handler_def)) { Rethrow(); return; } } /* If we get here then then handler *must* have been bound successfully, and * so have a non 'unknown' language. */ MCAssert(p_handler_def->language != kMCScriptForeignHandlerLanguageUnknown); // Fetch the handler signature. MCTypeInfoRef t_signature = GetSignatureOfHandler(p_instance, p_handler_def); // Determine whether the handler is variadic bool t_is_variadic = MCHandlerTypeInfoIsVariadic(t_signature); // Fetch the minimum parameter count (the fixed arg count for variadiac // handlers). uindex_t t_fixed_arg_count = MCHandlerTypeInfoGetParameterCount(t_signature); // Check the parameter count. if ((!t_is_variadic && t_fixed_arg_count != p_argument_regs.size()) || (t_is_variadic && t_fixed_arg_count > p_argument_regs.size())) { ThrowWrongNumberOfArguments(p_instance, p_handler_def, p_argument_regs.size()); return; } // Process the fixed arguments MCScriptForeignInvocation t_invocation; for(uindex_t i = 0; i < t_fixed_arg_count; ++i) { // Fetch the parameter mode and type MCHandlerTypeFieldMode t_param_mode = MCHandlerTypeInfoGetParameterMode(t_signature, i); MCTypeInfoRef t_param_type = MCHandlerTypeInfoGetParameterType(t_signature, i); // Process the argument if (!InvokeForeignArgument(t_invocation, p_instance, p_handler_def, i, t_param_mode, t_param_type, p_argument_regs[i])) { return; } } // If variadic, process the non-fixed arguments. if (t_is_variadic) { for(uindex_t i = t_fixed_arg_count; i < p_argument_regs.size(); i++) { if (!InvokeForeignVarArgument(t_invocation, p_instance, p_handler_def, i, p_argument_regs[i])) { return; } } } // Fetch the return value type. MCResolvedTypeInfo t_return_value_type; if (!ResolveTypeInfo(MCHandlerTypeInfoGetReturnType(t_signature), t_return_value_type)) { return; } // Compute the return value slot size, but only if the return type is not // nothing. size_t t_return_value_slot_size = 0; size_t t_return_value_slot_align = 0; __MCScriptValueDrop t_return_value_slot_drop = nil; void *t_return_value_slot_ptr = nil; if (t_return_value_type.named_type != kMCNullTypeInfo) { __MCScriptComputeSlotAttributes(t_return_value_type.type, t_return_value_slot_size, t_return_value_slot_align, t_return_value_slot_drop); // Alloate the return value slot storage (which will do nothing for // zero size). if (!t_invocation.Allocate(t_return_value_slot_size, t_return_value_slot_align, t_return_value_slot_ptr)) { Rethrow(); return; } } // Call the function - we assume here that if the invocation succeeds // then the result slot will be valid unless an LC error has been // raised. if (!t_invocation.Call(p_handler_def, t_signature, t_return_value_slot_ptr) || MCErrorIsPending()) { Rethrow(); return; } // Box the return value - this operation 'releases' the contents of // the slot (i.e. the value is taken by the box). MCAutoValueRef t_return_value; if (!BoxingConvert(t_return_value_type, t_return_value_slot_ptr, &t_return_value)) { return; } // Store the boxed return value into the result reg (if required). if (p_result_reg != UINDEX_MAX) { if (!CheckedStoreRegister(p_result_reg, *t_return_value)) { return; } } for(uindex_t i = 0; i < MCHandlerTypeInfoGetParameterCount(t_signature); ++i) { // Fetch the parameter mode and type MCHandlerTypeFieldMode t_param_mode = MCHandlerTypeInfoGetParameterMode(t_signature, i); MCTypeInfoRef t_param_type = MCHandlerTypeInfoGetParameterType(t_signature, i); // If the mode is in, there is nothing to do for this parameter. if (t_param_mode == kMCHandlerTypeFieldModeIn) { continue; } MCResolvedTypeInfo t_resolved_param_type; if (!ResolveTypeInfo(t_param_type, t_resolved_param_type)) { return; } // Do a boxing convert - note that we take the slot value from the // invocation as BoxingConvert will release regardless of successs. MCAutoValueRef t_arg_value; if (!BoxingConvert(t_resolved_param_type, t_invocation.TakeArgument(i), &t_arg_value)) { return; } // Finally do a checked store to register. if (!CheckedStoreRegister(p_argument_regs[i], *t_arg_value)) { return; } } } bool MCScriptExecuteContext::Bridge(MCValueRef p_value, MCValueRef& r_output_value) { // Get resolved typeinfo for the value. MCResolvedTypeInfo t_resolved_type; if (!ResolveTypeInfo(MCValueGetTypeInfo(p_value), t_resolved_type)) { return false; } // Get the foreign type descriptor for the value's type, if any. const MCForeignTypeDescriptor *t_desc = nullptr; if (MCTypeInfoIsForeign(t_resolved_type.type)) { t_desc = MCForeignTypeInfoGetDescriptor(t_resolved_type.type); } // If the type is not foreign or is not bridgeable foreign, just retain; // otherwise use doimport. if (t_desc == nullptr || t_desc->doimport == nullptr) { r_output_value = MCValueRetain(p_value); } else { if (!t_desc->doimport(t_desc, MCForeignValueGetContentsPtr(p_value), false, r_output_value)) { Rethrow(); return false; } } return true; } bool MCScriptExecuteContext::Convert(MCValueRef p_value, MCTypeInfoRef p_to_type, MCValueRef& r_new_value) { if (p_to_type == nil) { r_new_value = MCValueRetain(p_value); return true; } MCResolvedTypeInfo t_resolved_to_type; if (!ResolveTypeInfo(p_to_type, t_resolved_to_type)) { return false; } return ConvertToResolvedType(p_value, t_resolved_to_type, r_new_value); } bool MCScriptExecuteContext::ConvertToResolvedType(MCValueRef p_value, const MCResolvedTypeInfo& p_to_type, MCValueRef& r_new_value) { // Get resolved typeinfos. MCResolvedTypeInfo t_resolved_from_type; if (!ResolveTypeInfo(MCValueGetTypeInfo(p_value), t_resolved_from_type)) { return false; } // Check conformance - if this fails, then there is no conversion. if (!MCResolvedTypeInfoConforms(t_resolved_from_type, p_to_type)) { r_new_value = nil; return true; } // The only case which requires a value conversion is when // one side is foreign - which is a bridging operation. In // other cases, it is a cast towards root which doesn't require // a value conversion. const MCForeignTypeDescriptor *t_from_desc = nil; if (MCTypeInfoIsForeign(t_resolved_from_type.type)) { t_from_desc = MCForeignTypeInfoGetDescriptor(t_resolved_from_type.type); } const MCForeignTypeDescriptor *t_to_desc = nil; if (MCTypeInfoIsForeign(p_to_type.type)) { t_to_desc = MCForeignTypeInfoGetDescriptor(p_to_type.type); } if (t_from_desc == t_to_desc) { // The two types are the same or are both non-foreign - no conversion is // needed at this stage. r_new_value = MCValueRetain(p_value); |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |