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-instance.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-instance.cppMore file actions 1883 lines (1588 loc) · 58.6 KB / Copy pathscript-instance.cppFile metadata and controls1883 lines (1588 loc) · 58.6 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-2015 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 <foundation.h> #include <foundation-auto.h> #include <foundation-system.h> #include "ffi.h" #include "libscript/script.h" #include "script-private.h" #include "script-bytecode.hpp" #if defined(_MACOSX) || defined(TARGET_SUBPLATFORM_IPHONE) #include <objc/runtime.h> #endif //////////////////////////////////////////////////////////////////////////////// // This is the module of themost recent LCB stack frame on the current thread's // stack. It is set before and after a foreign handler call so that the native // code can get some element of context. static MCScriptModuleRef s_current_module = nil; static MCScriptWidgetEnterCallback s_widget_enter_callback = nullptr; static MCScriptWidgetLeaveCallback s_widget_leave_callback = nullptr; void MCScriptSetWidgetBarrierCallbacks(MCScriptWidgetEnterCallback p_entry, MCScriptWidgetLeaveCallback p_leave) { s_widget_enter_callback = p_entry; s_widget_leave_callback = p_leave; } //////////////////////////////////////////////////////////////////////////////// bool MCScriptCreateInstanceOfModule(MCScriptModuleRef p_module, MCScriptInstanceRef& r_instance) { bool t_success; t_success = true; MCScriptInstanceRef t_instance; t_instance = nil; // If the module is not usable, then we cannot create an instance. if (t_success) if (!p_module -> is_usable) return false; // If this is a module which shares a single instance, then return that if we have // one. if (p_module -> module_kind != kMCScriptModuleKindWidget && p_module -> shared_instance != nil) { r_instance = MCScriptRetainInstance(p_module -> shared_instance); return true; } // Attempt to create a script object. if (t_success) t_success = MCScriptCreateObject(kMCScriptObjectKindInstance, t_instance); // Now associate the script object with the module (so the 'slots' field make sense). if (t_success) { t_instance -> module = MCScriptRetainModule(p_module); } // Now allocate the slots field. if (t_success) t_success = MCMemoryNewArray(p_module -> slot_count, t_instance -> slots); if (t_success) { /* Loop over all of the variables of the module, initialising * the corresponding slot to a default value of the * appropriate type, if available. */ for (uindex_t i = 0; i < p_module->definition_count; ++i) { if (p_module->definitions[i]->kind != kMCScriptDefinitionKindVariable) continue; /* Not a variable */ MCScriptVariableDefinition *t_variable = static_cast<MCScriptVariableDefinition *>(p_module->definitions[i]); uindex_t t_slot_index = t_variable->slot_index; /* COMPUTE CHECK */ __MCScriptAssert__(t_slot_index < p_module->slot_count, "computed variable slot out of range"); MCTypeInfoRef t_type = p_module->types[t_variable->type]->typeinfo; if (nil == t_type) continue; /* Variable is untyped */ MCValueRef t_default = MCTypeInfoGetDefault(t_type); if (nil == t_default) continue; /* Type has no default value */ t_instance->slots[t_slot_index] = MCValueRetain(t_default); } // If this is a module which shares its instance, then add a link to it. // (Note this is weak reference - we don't retain, otherwise we would have // a cycle!). if (p_module -> module_kind != kMCScriptModuleKindWidget) p_module -> shared_instance = t_instance; r_instance = t_instance; } else MCScriptDestroyObject(t_instance); return t_success; } void MCScriptDestroyInstance(MCScriptInstanceRef self) { __MCScriptValidateObjectAndKind__(self, kMCScriptObjectKindInstance); // If the object had slots initialized, then free them. if (self -> slots != nil) { for(uindex_t i = 0; i < self -> module -> slot_count; i++) MCValueRelease(self -> slots[i]); MCMemoryDeleteArray(self -> slots); } // If the instance has handler-refs, then free them. for(uindex_t i = 0; i < self -> handler_count; i++) MCValueRelease(self -> handlers[i] . value); MCMemoryDeleteArray(self -> handlers); // If the instance has a module, and this is the shared instance then set // the shared instance field to nil. if (self -> module != nil && self -> module -> shared_instance == self) self -> module -> shared_instance = nil; // If the instance was associated with its module, then release it. if (self -> module != nil) MCScriptReleaseModule(self -> module); // The rest of the deallocation is handled by MCScriptDestroyObject. } //////////////////////////////////////////////////////////////////////////////// MCScriptInstanceRef MCScriptRetainInstance(MCScriptInstanceRef self) { __MCScriptValidateObjectAndKind__(self, kMCScriptObjectKindInstance); return (MCScriptInstanceRef)MCScriptRetainObject(self); } void MCScriptReleaseInstance(MCScriptInstanceRef self) { if (nil == self) return; __MCScriptValidateObjectAndKind__(self, kMCScriptObjectKindInstance); MCScriptReleaseObject(self); } //////////////////////////////////////////////////////////////////////////////// void MCScriptSetInstanceHostPtr(MCScriptInstanceRef self, void *p_host_ptr) { self->host_ptr = p_host_ptr; } void * MCScriptGetInstanceHostPtr(MCScriptInstanceRef self) { return self->host_ptr; } //////////////////////////////////////////////////////////////////////////////// MCScriptModuleRef MCScriptGetModuleOfInstance(MCScriptInstanceRef self) { return self -> module; } //////////////////////////////////////////////////////////////////////////////// static bool __MCHandlerTypeInfoConformsToPropertyGetter(MCTypeInfoRef p_typeinfo) { return MCHandlerTypeInfoGetParameterCount(p_typeinfo) == 0 && MCHandlerTypeInfoGetReturnType(p_typeinfo) != kMCNullTypeInfo; } static bool __MCHandlerTypeInfoConformsToPropertySetter(MCTypeInfoRef p_typeinfo) { return MCHandlerTypeInfoGetParameterCount(p_typeinfo) == 1 && MCHandlerTypeInfoGetParameterMode(p_typeinfo, 0) == kMCHandlerTypeFieldModeIn; } static bool __MCScriptCallHandlerDefinitionInInstance(MCScriptInstanceRef self, MCScriptHandlerDefinition *p_handler_def, MCValueRef *p_arguments, uindex_t p_argument_count, MCValueRef* r_value) { if (!self->module->licensed) return MCErrorThrowGeneric(MCSTR("extension not licensed")); void *t_cookie = nullptr; if (self->module->module_kind == kMCScriptModuleKindWidget) { if (s_widget_enter_callback != nullptr) { t_cookie = s_widget_enter_callback(self, self->host_ptr); } } MCScriptModuleRef t_previous = MCScriptSetCurrentModule(self->module); MCScriptExecuteContext t_execute_ctxt; t_execute_ctxt.Enter(self, p_handler_def, MCMakeSpan(p_arguments, p_argument_count), r_value); while(t_execute_ctxt.Step()) { MCScriptBytecodeExecute(t_execute_ctxt); } if (self->module->module_kind == kMCScriptModuleKindWidget) { if (s_widget_leave_callback != nullptr) { s_widget_leave_callback(self, self->host_ptr, t_cookie); } } MCScriptSetCurrentModule(t_previous); if (!t_execute_ctxt.Leave()) { return false; } return true; } static bool __MCScriptGetVariablePropertyInInstance(MCScriptInstanceRef self, MCScriptVariableDefinition *p_variable_def, MCValueRef& r_value) { MCValueRef t_value = self->slots[p_variable_def->slot_index]; r_value = t_value != nil ? MCValueRetain(t_value) : t_value; return true; } static bool __MCScriptGetHandlerPropertyInInstance(MCScriptInstanceRef self, MCScriptHandlerDefinition *p_handler_def, MCValueRef& r_value) { /* LOAD CHECK */ __MCScriptAssert__(__MCHandlerTypeInfoConformsToPropertyGetter(self->module->types[p_handler_def->type]->typeinfo), "incorrect signature for property getter"); return __MCScriptCallHandlerDefinitionInInstance(self, p_handler_def, nil, 0, &r_value); } static bool __MCScriptSetVariablePropertyInInstance(MCScriptInstanceRef self, MCScriptVariableDefinition *p_variable_def, MCValueRef p_value) { // BRIDGE: This should bridge (convert) the type if necessary. MCValueAssign(self->slots[p_variable_def->slot_index], p_value); return true; } static bool __MCScriptSetHandlerPropertyInInstance(MCScriptInstanceRef self, MCScriptHandlerDefinition *p_handler_def, MCValueRef p_value) { /* LOAD CHECK */ __MCScriptAssert__(__MCHandlerTypeInfoConformsToPropertySetter(self->module->types[p_handler_def->type]->typeinfo), "incorrect signature for property setter"); return __MCScriptCallHandlerDefinitionInInstance(self, p_handler_def, &p_value, 1, nil); } bool MCScriptGetPropertyInInstance(MCScriptInstanceRef self, MCNameRef p_property, MCValueRef& r_value) { __MCScriptValidateObjectAndKind__(self, kMCScriptObjectKindInstance); // Lookup the definition (throws if not found). MCScriptPropertyDefinition *t_property_def; if (!MCScriptLookupPropertyDefinitionInModule(self->module, p_property, t_property_def)) { return MCScriptThrowPropertyNotFoundError(self, p_property); } MCScriptDefinition *t_getter; t_getter = t_property_def->getter != 0 ? self->module->definitions[t_property_def->getter - 1] : nil; /* LOAD CHECK */ __MCScriptAssert__(t_getter != nil, "property has no getter"); switch(t_getter->kind) { case kMCScriptDefinitionKindVariable: { MCValueRef t_value; if (!__MCScriptGetVariablePropertyInInstance(self, static_cast<MCScriptVariableDefinition *>(t_getter), t_value)) { return false; } if (t_value == nil) { return MCScriptThrowPropertyUsedBeforeAssignedError(self, t_property_def); } r_value = t_value; } break; case kMCScriptDefinitionKindHandler: { if (!__MCScriptGetHandlerPropertyInInstance(self, static_cast<MCScriptHandlerDefinition *>(t_getter), r_value)) { return false; } } break; default: /* LOAD CHECK */ __MCScriptUnreachable__("property getter is not a variable or handler"); break; } return true; } bool MCScriptSetPropertyInInstance(MCScriptInstanceRef self, MCNameRef p_property, MCValueRef p_value) { __MCScriptValidateObjectAndKind__(self, kMCScriptObjectKindInstance); // Lookup the definition (throws if not found). MCScriptPropertyDefinition *t_property_def; if (!MCScriptLookupPropertyDefinitionInModule(self->module, p_property, t_property_def)) { return MCScriptThrowPropertyNotFoundError(self, p_property); } MCScriptDefinition *t_setter; t_setter = t_property_def->setter != 0 ? self->module->definitions[t_property_def->setter - 1] : nil; // If there is no setter for the property then this is an error. if (t_setter == nil) return MCScriptThrowCannotSetReadOnlyPropertyError(self, p_property); /* LOAD CHECK */ __MCScriptAssert__(t_setter != nil, "property has no setter"); /* LOAD CHECK */ __MCScriptAssert__(t_setter->kind == kMCScriptDefinitionKindVariable || t_setter->kind == kMCScriptDefinitionKindHandler, "property setter is not a variable or handler"); MCTypeInfoRef t_property_type; switch(t_setter->kind) { case kMCScriptDefinitionKindVariable: { t_property_type = self->module->types[static_cast<MCScriptVariableDefinition*>(t_setter)->type]->typeinfo; } break; case kMCScriptDefinitionKindHandler: { MCTypeInfoRef t_setter_signature = self->module->types[static_cast<MCScriptHandlerDefinition*>(t_setter)->type]->typeinfo; /* LOAD CHECK */ __MCScriptAssert__(__MCHandlerTypeInfoConformsToPropertySetter(t_setter_signature), "incorrect signature for property setter"); t_property_type = MCHandlerTypeInfoGetParameterType(t_setter_signature, 0); } break; default: { __MCScriptUnreachable__("inconsistency with definition kind in property setting"); return false; } break; } // Check the type passed in conforms to the required type of the // property here so that it generates a more useful error message. if (!MCTypeInfoConforms(MCValueGetTypeInfo(p_value), t_property_type)) { return MCScriptThrowInvalidValueForPropertyError(self, t_property_def, t_property_type, p_value); } switch(t_setter->kind) { case kMCScriptDefinitionKindVariable: { if (!__MCScriptSetVariablePropertyInInstance(self, static_cast<MCScriptVariableDefinition *>(t_setter), p_value)) { return false; } } break; case kMCScriptDefinitionKindHandler: { if (!__MCScriptSetHandlerPropertyInInstance(self, static_cast<MCScriptHandlerDefinition *>(t_setter), p_value)) { return false; } } break; default: { __MCScriptUnreachable__("inconsistency with definition kind in property setting"); return false; } break; } return true; } bool MCScriptCallHandlerInInstanceInternal(MCScriptInstanceRef self, MCScriptHandlerDefinition *p_handler_def, MCValueRef *p_arguments, uindex_t p_argument_count, MCValueRef& r_value) { return __MCScriptCallHandlerDefinitionInInstance(self, p_handler_def, p_arguments, p_argument_count, &r_value); } bool MCScriptCallHandlerInInstance(MCScriptInstanceRef self, MCNameRef p_handler, MCValueRef *p_arguments, uindex_t p_argument_count, MCValueRef& r_value) { __MCScriptValidateObjectAndKind__(self, kMCScriptObjectKindInstance); MCScriptHandlerDefinition *t_handler_def; if (!MCScriptLookupHandlerDefinitionInModule(self->module, p_handler, t_handler_def)) { return MCScriptThrowHandlerNotFoundError(self, p_handler); } return MCScriptCallHandlerInInstanceInternal(self, t_handler_def, p_arguments, p_argument_count, r_value); } bool MCScriptCallHandlerInInstanceIfFound(MCScriptInstanceRef self, MCNameRef p_handler, MCValueRef *p_arguments, uindex_t p_argument_count, MCValueRef& r_value) { __MCScriptValidateObjectAndKind__(self, kMCScriptObjectKindInstance); MCScriptHandlerDefinition *t_handler_def; if (!MCScriptLookupHandlerDefinitionInModule(self->module, p_handler, t_handler_def)) { r_value = MCValueRetain(kMCNull); return true; } return MCScriptCallHandlerInInstanceInternal(self, t_handler_def, p_arguments, p_argument_count, r_value); } //////////////////////////////////////////////////////////////////////////////// // This method resolves the binding string in the foreign function. The format is: // [lang:][library>][class.]function[!calling] // // lang - one of c, cpp, objc or java. If not present, it is taken to be c. // library - the library to load the symbol from. If not present, it is taken to be the // main executable. // class - only valid for cpp, objc, or java. If not present, it is taken to be no class. // function - the name of the function or method. // calling - the calling convention, if not present it is taken to be the platform default. // // The calling conventions are: // default (sysv on 32-bit unix intel, unix64 on 64-bit unix intel, win64 on 64-bit windows, cdelc on 32-bit windows) // stdcall (windows 32-bit) // thiscall (windows 32-bit) // fastcall (windows 32-bit) // cdecl (windows 32-bit) // pascal (windows 32-bit) // register (windows 32-bit) // If a windows calling convention is specified, it is taken to be 'default' on // other platforms. // // Split x_string at p_char, returning the portion up to the char, or the end // of the string in r_field. The source string x_string is replaced with the // remainder of the string. // i.e. Split("foo.bar", '.'): // x_string = "bar", r_field = "foo" // static bool __MCScriptSplitForeignBindingString(MCStringRef& x_string, codepoint_t p_char, MCStringRef& r_field) { MCStringRef t_head, t_tail; if (!MCStringDivideAtChar(x_string, p_char, kMCStringOptionCompareExact, t_head, t_tail)) { return false; } MCValueRelease(x_string); if (!MCStringIsEmpty(t_tail)) { r_field = t_head; x_string = t_tail; } else { r_field = t_tail; x_string = t_head; } return true; } static bool __split_function_signature(MCStringRef p_string, MCStringRef& r_function, MCStringRef& r_arguments, MCStringRef& r_return) { MCAutoStringRef t_head, t_args, t_return; uindex_t t_open_bracket_offset, t_close_bracket_offset; if (!MCStringFirstIndexOfChar(p_string, '(', 0, kMCStringOptionCompareExact, t_open_bracket_offset) || !MCStringFirstIndexOfChar(p_string, ')', 0, kMCStringOptionCompareExact, t_close_bracket_offset)) { r_function = MCValueRetain(p_string); r_arguments = MCValueRetain(kMCEmptyString); r_return = MCValueRetain(kMCEmptyString); return true; } if (!MCStringCopySubstring(p_string, MCRangeMakeMinMax(t_open_bracket_offset, t_close_bracket_offset + 1), &t_args)) return false; if (!MCStringCopySubstring(p_string, MCRangeMake(t_close_bracket_offset + 1, UINDEX_MAX), &t_return)) return false; if (!MCStringCopySubstring(p_string, MCRangeMake(0, t_open_bracket_offset), &t_head)) return false; r_arguments = MCValueRetain(*t_args); r_return = MCValueRetain(*t_return); r_function = MCValueRetain(*t_head); return true; } // Resolve the call type static MCJavaCallType __MCScriptGetJavaCallType(MCStringRef p_class, MCStringRef p_function, MCStringRef p_calling) { if (MCStringIsEqualToCString(p_function, "new", kMCStringOptionCompareExact)) return MCJavaCallTypeConstructor; if (MCStringIsEqualToCString(p_function, "interface", kMCStringOptionCompareExact)) return MCJavaCallTypeInterfaceProxy; bool t_is_static = MCStringIsEqualToCString(p_calling, "static", kMCStringOptionCompareCaseless); // If the 'class' is get or set, then the binding is for a field if (p_class != nullptr && MCStringIsEqualToCString(p_class, "get", kMCStringOptionCompareExact)) { if (t_is_static) return MCJavaCallTypeStaticGetter; return MCJavaCallTypeGetter; } if (p_class != nullptr && MCStringIsEqualToCString(p_class, "set", kMCStringOptionCompareExact)) { if (t_is_static) return MCJavaCallTypeStaticSetter; return MCJavaCallTypeSetter; } if (t_is_static) return MCJavaCallTypeStatic; if (MCStringIsEqualToCString(p_calling, "nonvirtual", kMCStringOptionCompareCaseless)) return MCJavaCallTypeNonVirtual; if (MCStringIsEmpty(p_calling) || MCStringIsEqualToCString(p_calling, "instance", kMCStringOptionCompareCaseless)) return MCJavaCallTypeInstance; return MCJavaCallTypeUnknown; } // Resolve the call type static bool __MCScriptGetThreadAffinity(MCStringRef p_thread, MCScriptThreadAffinity &r_thread) { if (MCStringIsEmpty(p_thread)) { r_thread = kMCScriptThreadAffinityDefault; return true; } if (MCStringIsEqualToCString(p_thread, "ui", kMCStringOptionCompareCaseless)) { r_thread = kMCScriptThreadAffinityUI; return true; } return MCScriptThrowUnknownThreadAffinityError(); } static bool __MCScriptResolveForeignFunctionBindingForC(MCScriptInstanceRef p_instance, MCScriptForeignHandlerDefinition *p_handler, MCScriptForeignHandlerInfoRef p_info, bool* r_bound) { MCSLibraryRef t_module; if (!MCScriptLoadModuleLibrary(MCScriptGetModuleOfInstance(p_instance), p_info->c.library, t_module)) { if (r_bound == nil) { return MCScriptThrowUnableToLoadForiegnLibraryError(); } *r_bound = false; return true; } /* Resolve the symbol from the module which we've loaded */ void *t_pointer = MCSLibraryLookupSymbol(t_module, p_info->c.function); /* A nullptr pointer means that the symbol doesn't exist, so we either * throw an error (if in 'must' bind mode - r_bound == nullptr) or * indicate we succeeded, but the binding failed due to not finding the * symbol (r_bound != nullptr). */ if (t_pointer == nullptr) { if (r_bound == nullptr) { return MCScriptThrowUnableToResolveForeignHandlerError(p_instance, p_handler); } *r_bound = false; return true; } /* Now we must compute the libffi CIF for this C language handler. If * this fails, it is an error - not a failure to bind - as it indicates * OOM or something went wrong in libffi. */ if (!MCHandlerTypeInfoGetLayoutType(p_instance->module->types[p_handler->type]->typeinfo, p_info->c.call_type, p_handler->c.function_cif)) { /* The above call already throws an appropriate error, so we can * just return false. */ return false; } p_handler->c.function = t_pointer; if (r_bound != nullptr) { *r_bound = true; } return true; } #if defined(_MACOSX) || defined(TARGET_SUBPLATFORM_IPHONE) /* objc:library>class.method[?thread] */ static bool __MCScriptResolveForeignFunctionBindingForObjC(MCScriptInstanceRef p_instance, MCScriptForeignHandlerDefinition *p_handler, MCScriptForeignHandlerInfoRef p_info, bool* r_bound) { MCSLibraryRef t_module; if (!MCScriptLoadModuleLibrary(MCScriptGetModuleOfInstance(p_instance), p_info->objc.library, t_module)) { if (r_bound == nil) { return MCScriptThrowUnableToLoadForiegnLibraryError(); } *r_bound = false; return true; } MCAutoStringRefAsUTF8String t_selector_name_cstring; if (!t_selector_name_cstring.Lock(p_info->objc.method_name)) { return false; } bool t_valid = true; bool t_is_dynamic = MCStringIsEmpty(p_info->objc.class_name); bool t_is_class = p_info->objc.call_type == kMCScriptForeignHandlerObjcCallTypeClassMethod; /* Lookup the class, to make sure it exists. */ Class t_objc_class = nullptr; if (t_valid) { if (t_is_dynamic) { if (t_is_class) { /* ERROR: should be can't dynamically bind to class methods */ t_valid = false; } } else { MCAutoStringRefAsUTF8String t_class_cstring; if (!t_class_cstring.Lock(p_info->objc.class_name)) { return false; } t_objc_class = objc_getClass(*t_class_cstring); if (t_objc_class == nullptr) { /* ERROR: should be class not found */ t_valid = false; } } } /* Lookup the selector, if this doesn't work then we're not going to get * very far! (OOM, probably). */ SEL t_objc_sel = sel_registerName(*t_selector_name_cstring); if (t_valid && t_objc_sel == nullptr) { /* ERROR: objc runtime failure */ t_valid = false; } uindex_t t_required_args = 0; /* Get the Method - either class or instance - from the class */ if (t_valid && !t_is_dynamic) { if (!t_is_class) { Method t_method_info = class_getInstanceMethod(t_objc_class, t_objc_sel); if (t_method_info != nullptr) { t_required_args = method_getNumberOfArguments(t_method_info); } else { t_valid = false; } /* If a lookup fails, then check to see if it could be a property * binding. Classes such as NSUserNotification declare dynamic props * which means that the methods aren't present until runtime, but * the prop definitions are. */ if (!t_valid && MCStringBeginsWithCString(p_info->objc.method_name, (const char_t *)"set", kMCStringOptionCompareExact) && MCStringEndsWithCString(p_info->objc.method_name, (const char_t *)":", kMCStringOptionCompareExact)) { if (MCStringGetLength(p_info->objc.method_name) < 255) { char t_prop_name[256]; strncpy(t_prop_name, *t_selector_name_cstring + 3, strlen(*t_selector_name_cstring) - 4); t_prop_name[0] = tolower(t_prop_name[0]); t_prop_name[strlen(*t_selector_name_cstring) - 4] = '\0'; objc_property_t t_property = class_getProperty(t_objc_class, t_prop_name); if (t_property != nullptr) { t_valid = strstr(property_getAttributes(t_property), ",R,") == nullptr; } } t_required_args = 3; } else if (!t_valid && MCStringCountChar(p_info->objc.method_name, MCRangeMake(0, MCStringGetLength(p_info->objc.method_name)), ':', kMCStringOptionCompareExact) == 0) { objc_property_t t_property = class_getProperty(t_objc_class, *t_selector_name_cstring); if (t_property != nullptr) { t_valid = true; } t_required_args = 2; } } else { Method t_method_info = class_getClassMethod(t_objc_class, t_objc_sel); if (t_method_info != nullptr) { t_required_args = method_getNumberOfArguments(t_method_info); } else { t_valid = false; } } } /* Next we must compute the cif. */ MCAutoCustomPointer<void, MCMemoryDeallocate> t_layout_cif; ffi_cif *t_cif = nullptr; ffi_type *t_cif_return_type = nullptr; ffi_type **t_cif_arg_types = nullptr; unsigned int t_arg_count = 0; if (t_valid) { MCTypeInfoRef t_signature = p_instance->module->types[p_handler->type]->typeinfo; /* The number of arguments used to call the bound handler */ uindex_t t_user_arg_count = MCHandlerTypeInfoGetParameterCount(t_signature); /* The number of arguments used to pass to objc_msgSend. This is 1 more * if an instance method (the selector); but is 2 more if a class method * (the class object and the selector). */ t_arg_count = t_user_arg_count + (t_is_class ? 2 : 1); MCTypeInfoRef t_return_type = MCHandlerTypeInfoGetReturnType(t_signature); t_valid = t_arg_count == t_required_args || t_is_dynamic; MCResolvedTypeInfo t_resolved_return_type; if (!MCTypeInfoResolve(t_return_type, t_resolved_return_type)) { t_valid = false; } else if (t_valid) { if (t_resolved_return_type.named_type == kMCNullTypeInfo) { t_cif_return_type = &ffi_type_void; } else if (MCTypeInfoIsForeign(t_resolved_return_type.type)) { t_cif_return_type = (ffi_type *)MCForeignTypeInfoGetLayoutType(t_resolved_return_type.type); } else { t_cif_return_type = &ffi_type_pointer; } } unsigned int t_user_arg_index = 0; unsigned int t_arg_index = 0; /* Allocate memory for the handler layout (array of ffi_type*) and * ffi_cif. This is a single block which will contain the cif followed * by the type array. */ if (t_valid) { if (!MCMemoryAllocate(sizeof(ffi_type*) * t_arg_count + sizeof(ffi_cif), &t_layout_cif)) { /* ERROR: OOM */ t_valid = false; } else { t_cif = static_cast<ffi_cif *>(*t_layout_cif); t_cif_arg_types = reinterpret_cast<ffi_type **>(t_cif + 1); /* The first argument is always of pointer type - the object ptr. This * is only present in the user signature if it is an instance method.*/ t_cif_arg_types[t_arg_index] = &ffi_type_pointer; t_arg_index += 1; if (!t_is_class) { |
| 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. |