Changeset 55 for NEWT0/trunk/contrib
- Timestamp:
- 05/26/06 19:14:30 (3 years ago)
- Location:
- NEWT0/trunk/contrib/NativeCalls
- Files:
-
- 2 modified
-
NativeCalls.c (modified) (12 diffs)
-
native.newt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
NEWT0/trunk/contrib/NativeCalls/NativeCalls.c
r31 r55 495 495 theResult = false; 496 496 } 497 } else if (NewtSymbolEqual(inNSType, NSSYM(string))) { 497 } else if ((NewtSymbolEqual(inNSType, NSSYM(string))) 498 || (NewtSymbolEqual(inNSType, NSSYM(iostring)))) { 498 499 *outFFIType = &ffi_type_pointer; 499 500 if (NewtRefIsString(inNSValue)) … … 505 506 theResult = false; 506 507 } 507 } else if (NewtSymbolEqual(inNSType, NSSYM(binary))) { 508 } else if ((NewtSymbolEqual(inNSType, NSSYM(binary))) 509 || (NewtSymbolEqual(inNSType, NSSYM(iobinary)))) { 508 510 *outFFIType = &ffi_type_pointer; 509 511 if (NewtRefIsBinary(inNSValue)) … … 512 514 (void*) NewtRefToBinary(inNSValue); 513 515 } else { 514 (void) NewtThrow(kNErrNotA String, inNSValue);516 (void) NewtThrow(kNErrNotABinaryObject, inNSValue); 515 517 theResult = false; 516 518 } … … 736 738 737 739 /** 738 * Native function lib: DefineGlobalFn(specs)740 * Native function lib:GetFunction(specs) 739 741 * 740 742 * specs is a frame defining the native function. It includes the following … … 742 744 * 743 745 * name (string) name of the native function to call 744 * symbol (symbol) optional name of the global function to define. If not745 * present (or nil), the global function will have the same name as746 * the native function. Use this when names conflicts (e.g. strlen747 * and StrLen)748 746 * args (array of symbols) types of the arguments. 749 747 * result (symbol) type of the result. … … 767 765 * 'longdouble real ffi_type_longdouble 768 766 * 'string string ffi_type_pointer 767 * 'iostring string ffi_type_pointer not available for result 769 768 * 'binary binary ffi_type_pointer not available for result 769 * 'iobinary binary ffi_type_pointer not available for result 770 770 * 'pointer int or binary ffi_type_pointer 771 771 * 772 772 * Structures aren't supported yet. 773 * 774 * The symbol of the function is added to a list of the library object to allow 775 * Close to undefine the function. 773 * string and iostring are identical (strings are I/O). Same with binary and 774 * iobinary. This may change in the future. 776 775 * 777 776 * @param inRcvr self … … 780 779 */ 781 780 newtRef 782 DefineGlobalFn(781 GetFunction( 783 782 newtRefArg inRcvr, 784 783 newtRefArg inSpec) … … 787 786 newtRefVar resultType; 788 787 newtRefVar functionName; 789 newtRefVar functionSymbol;790 788 newtRefVar functionObject; 791 newtRefVar libList;792 789 793 790 /* check self */ … … 820 817 { 821 818 return NewtThrow(kNErrNotAString, functionName); 822 }823 functionSymbol = NcGetSlot(inSpec, NSSYM(symbol));824 if (NewtRefIsNIL(functionSymbol))825 {826 functionSymbol = NewtMakeSymbol(NewtRefToString(functionName));827 } else if (!NewtRefIsSymbol(functionSymbol)) {828 return NewtThrow(kNErrNotASymbol, functionSymbol);829 }830 831 /* check the function doesn't exist yet */832 if (NewtHasGlobalFn(functionSymbol))833 {834 return NewtThrow(835 kNErrNative,836 NewtMakeString("Global function already exists", true));837 819 } 838 820 … … 847 829 NcSetSlot(functionObject, NSSYM(_argTypes), argTypes); 848 830 831 return functionObject; 832 } 833 834 /** 835 * Native function lib:DefGlobalFn(symbol, specs) 836 * 837 * symbol is the symbol of the function to define. 838 * specs is a frame defining the native function. It is passed to GetFunction. 839 * 840 * The symbol of the function is added to a list of the library object to allow 841 * Close to undefine the function. 842 * 843 * @param inRcvr self 844 * @param inSymbol function symbol 845 * @param inSpec specification frame 846 * @return NIL 847 */ 848 newtRef 849 DefGlobalFn( 850 newtRefArg inRcvr, 851 newtRefArg inSymbol, 852 newtRefArg inSpec) 853 { 854 newtRefVar functionObject; 855 newtRefVar libList; 856 857 /* check the function doesn't exist yet */ 858 if (NewtHasGlobalFn(inSymbol)) 859 { 860 return NewtThrow( 861 kNErrNative, 862 NewtMakeString("Global function already exists", true)); 863 } 864 865 /* Get the object */ 866 functionObject = GetFunction(inRcvr, inSpec); 867 849 868 /* add the function to the list */ 850 869 libList = NcGetSlot(inRcvr, NSSYM(_globalFns)); … … 854 873 NcSetSlot(inRcvr, NSSYM(_globalFns), libList); 855 874 } 856 NcAddArraySlot(libList, functionSymbol);875 NcAddArraySlot(libList, inSymbol); 857 876 858 877 /* define the global function */ 859 NcDefGlobalFn( functionSymbol, functionObject);860 861 return kNewtRefNIL;878 NcDefGlobalFn(inSymbol, functionObject); 879 880 return inSymbol; 862 881 } 863 882 … … 959 978 Close, 0, "Close()")); 960 979 NcSetSlot(magicPtr, 961 NSSYM( DefineGlobalFn),980 NSSYM(GetFunction), 962 981 NewtMakeNativeFunc( 963 DefineGlobalFn, 1, "DefineGlobalFn(specs)")); 982 GetFunction, 1, "GetFunction(spec)")); 983 NcSetSlot(magicPtr, 984 NSSYM(DefGlobalFn), 985 NewtMakeNativeFunc( 986 DefGlobalFn, 2, "DefGlobalFn(symbol, spec)")); 964 987 965 988 NcDefMagicPointer(kLibParentMagicPtrKey, magicPtr); -
NEWT0/trunk/contrib/NativeCalls/native.newt
r31 r55 12 12 13 13 local libc := OpenNativeLibrary("libc"); 14 libc:Def ineGlobalFn({15 name: "gethostname",16 symbol: '|libc.gethostname|,14 libc:DefGlobalFn( 15 '|libc.gethostname|, 16 {name: "gethostname", 17 17 args: ['string, 'sint32], 18 18 result: 'sint32}); 19 libc:Def ineGlobalFn({20 name: "socket",21 symbol: '|libc.socket|,19 libc:DefGlobalFn( 20 '|libc.socket|, 21 {name: "socket", 22 22 args: ['sint32, 'sint32, 'sint32], 23 23 result: 'sint32}); 24 libc:DefineGlobalFn({ 25 name: "close", 26 symbol: '|libc.close|, 24 libc:DefGlobalFn( 25 '|libc.close|, 26 {name: "close", 27 args: ['sint32], 28 result: 'sint32}); 29 libc:DefGlobalFn( 30 '|libc.popen|, 31 {name: "popen", 32 args: ['string, 'string], 33 result: 'sint32}); 34 libc:DefGlobalFn( 35 '|libc.pclose|, 36 {name: "pclose", 27 37 args: ['sint32], 28 38 result: 'sint32}); 29 39 30 40 local libm := OpenNativeLibrary("libm"); 31 libm:Def ineGlobalFn({32 name: "cos",33 symbol: '|libm.cos|,41 libm:DefGlobalFn( 42 '|libm.cos|, 43 {name: "cos", 34 44 args: ['double], 35 45 result: 'double}); 36 46 37 47 local libcurses := OpenNativeLibrary("libcurses"); 38 libcurses:Def ineGlobalFn({39 name: "initscr",40 symbol: '|libcurses.initscr|,48 libcurses:DefGlobalFn( 49 '|libcurses.initscr|, 50 {name: "initscr", 41 51 args: [], 42 52 result: 'pointer}); 43 libcurses:Def ineGlobalFn({44 name: "cbreak",45 symbol: '|libcurses.cbreak|,53 libcurses:DefGlobalFn( 54 '|libcurses.cbreak|, 55 {name: "cbreak", 46 56 args: [], 47 57 result: 'sint32}); 48 libcurses:Def ineGlobalFn({49 name: "noecho",50 symbol: '|libcurses.noecho|,58 libcurses:DefGlobalFn( 59 '|libcurses.noecho|, 60 {name: "noecho", 51 61 args: [], 52 62 result: 'sint32}); 53 libcurses:Def ineGlobalFn({54 name: "flash",55 symbol: '|libcurses.flash|,63 libcurses:DefGlobalFn( 64 '|libcurses.flash|, 65 {name: "flash", 56 66 args: [], 57 67 result: 'sint32}); 58 libcurses:Def ineGlobalFn({59 name: "endwin",60 symbol: '|libcurses.endwin|,68 libcurses:DefGlobalFn( 69 '|libcurses.endwin|, 70 {name: "endwin", 61 71 args: [], 62 72 result: 'sint32}); … … 111 121 |libcurses.endwin|(); 112 122 123 // call open(1) 124 local popenref := |libc.popen|("open /Applications/TextEdit.app/", "r"); 125 |libc.pclose|(popenref); 126 113 127 libm:Close(); 114 128 libc:Close();
