Changeset 55 for NEWT0/trunk

Show
Ignore:
Timestamp:
05/26/06 19:14:30 (2 years ago)
Author:
pguyot
Message:

Change in the APIs to be compatible with Relativity API.
(actually, use of :DefGlobalFn(symbol, spec) is discouraged in Relativity)

Location:
NEWT0/trunk/contrib/NativeCalls
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • NEWT0/trunk/contrib/NativeCalls/NativeCalls.c

    r31 r55  
    495495            theResult = false; 
    496496        } 
    497     } else if (NewtSymbolEqual(inNSType, NSSYM(string))) { 
     497    } else if ((NewtSymbolEqual(inNSType, NSSYM(string))) 
     498        || (NewtSymbolEqual(inNSType, NSSYM(iostring)))) { 
    498499        *outFFIType = &ffi_type_pointer; 
    499500        if (NewtRefIsString(inNSValue)) 
     
    505506            theResult = false; 
    506507        } 
    507     } else if (NewtSymbolEqual(inNSType, NSSYM(binary))) { 
     508    } else if ((NewtSymbolEqual(inNSType, NSSYM(binary))) 
     509        || (NewtSymbolEqual(inNSType, NSSYM(iobinary)))) { 
    508510        *outFFIType = &ffi_type_pointer; 
    509511        if (NewtRefIsBinary(inNSValue)) 
     
    512514                (void*) NewtRefToBinary(inNSValue); 
    513515        } else { 
    514             (void) NewtThrow(kNErrNotAString, inNSValue); 
     516            (void) NewtThrow(kNErrNotABinaryObject, inNSValue); 
    515517            theResult = false; 
    516518        } 
     
    736738 
    737739/** 
    738  * Native function lib:DefineGlobalFn(specs) 
     740 * Native function lib:GetFunction(specs) 
    739741 * 
    740742 * specs is a frame defining the native function. It includes the following 
     
    742744 * 
    743745 * name         (string) name of the native function to call 
    744  * symbol       (symbol) optional name of the global function to define. If not 
    745  *              present (or nil), the global function will have the same name as 
    746  *              the native function. Use this when names conflicts (e.g. strlen 
    747  *              and StrLen) 
    748746 * args         (array of symbols) types of the arguments. 
    749747 * result       (symbol) type of the result. 
     
    767765 *  'longdouble real                ffi_type_longdouble 
    768766 *  'string     string              ffi_type_pointer 
     767 *  'iostring   string              ffi_type_pointer        not available for result 
    769768 *  'binary     binary              ffi_type_pointer        not available for result 
     769 *  'iobinary   binary              ffi_type_pointer        not available for result 
    770770 *  'pointer    int or binary       ffi_type_pointer 
    771771 * 
    772772 * 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. 
    776775 * 
    777776 * @param inRcvr    self 
     
    780779 */ 
    781780newtRef 
    782 DefineGlobalFn( 
     781GetFunction( 
    783782    newtRefArg inRcvr, 
    784783    newtRefArg inSpec) 
     
    787786    newtRefVar resultType; 
    788787    newtRefVar functionName; 
    789     newtRefVar functionSymbol; 
    790788    newtRefVar functionObject; 
    791     newtRefVar libList; 
    792789 
    793790    /* check self */ 
     
    820817    { 
    821818        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)); 
    837819    } 
    838820     
     
    847829    NcSetSlot(functionObject, NSSYM(_argTypes), argTypes); 
    848830     
     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 */ 
     848newtRef 
     849DefGlobalFn( 
     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     
    849868    /* add the function to the list */ 
    850869    libList = NcGetSlot(inRcvr, NSSYM(_globalFns)); 
     
    854873        NcSetSlot(inRcvr, NSSYM(_globalFns), libList); 
    855874    } 
    856     NcAddArraySlot(libList, functionSymbol); 
     875    NcAddArraySlot(libList, inSymbol); 
    857876     
    858877    /* define the global function */ 
    859     NcDefGlobalFn(functionSymbol, functionObject); 
    860  
    861     return kNewtRefNIL; 
     878    NcDefGlobalFn(inSymbol, functionObject); 
     879     
     880    return inSymbol; 
    862881} 
    863882 
     
    959978            Close, 0, "Close()")); 
    960979    NcSetSlot(magicPtr, 
    961         NSSYM(DefineGlobalFn), 
     980        NSSYM(GetFunction), 
    962981        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)")); 
    964987 
    965988    NcDefMagicPointer(kLibParentMagicPtrKey, magicPtr); 
  • NEWT0/trunk/contrib/NativeCalls/native.newt

    r31 r55  
    1212 
    1313    local libc := OpenNativeLibrary("libc"); 
    14     libc:DefineGlobalFn({ 
    15         name: "gethostname", 
    16         symbol: '|libc.gethostname|, 
     14    libc:DefGlobalFn( 
     15        '|libc.gethostname|, 
     16        {name: "gethostname", 
    1717        args: ['string, 'sint32], 
    1818        result: 'sint32}); 
    19     libc:DefineGlobalFn({ 
    20         name: "socket", 
    21         symbol: '|libc.socket|, 
     19    libc:DefGlobalFn( 
     20        '|libc.socket|, 
     21        {name: "socket", 
    2222        args: ['sint32, 'sint32, 'sint32], 
    2323        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", 
    2737        args: ['sint32], 
    2838        result: 'sint32}); 
    2939 
    3040    local libm := OpenNativeLibrary("libm"); 
    31     libm:DefineGlobalFn({ 
    32         name: "cos", 
    33         symbol: '|libm.cos|, 
     41    libm:DefGlobalFn( 
     42        '|libm.cos|, 
     43        {name: "cos", 
    3444        args: ['double], 
    3545        result: 'double}); 
    3646 
    3747    local libcurses := OpenNativeLibrary("libcurses"); 
    38     libcurses:DefineGlobalFn({ 
    39         name: "initscr", 
    40         symbol: '|libcurses.initscr|, 
     48    libcurses:DefGlobalFn( 
     49        '|libcurses.initscr|, 
     50        {name: "initscr", 
    4151        args: [], 
    4252        result: 'pointer}); 
    43     libcurses:DefineGlobalFn({ 
    44         name: "cbreak", 
    45         symbol: '|libcurses.cbreak|, 
     53    libcurses:DefGlobalFn( 
     54        '|libcurses.cbreak|, 
     55        {name: "cbreak", 
    4656        args: [], 
    4757        result: 'sint32}); 
    48     libcurses:DefineGlobalFn({ 
    49         name: "noecho", 
    50         symbol: '|libcurses.noecho|, 
     58    libcurses:DefGlobalFn( 
     59        '|libcurses.noecho|, 
     60        {name: "noecho", 
    5161        args: [], 
    5262        result: 'sint32}); 
    53     libcurses:DefineGlobalFn({ 
    54         name: "flash", 
    55         symbol: '|libcurses.flash|, 
     63    libcurses:DefGlobalFn( 
     64        '|libcurses.flash|, 
     65        {name: "flash", 
    5666        args: [], 
    5767        result: 'sint32}); 
    58     libcurses:DefineGlobalFn({ 
    59         name: "endwin", 
    60         symbol: '|libcurses.endwin|, 
     68    libcurses:DefGlobalFn( 
     69        '|libcurses.endwin|, 
     70        {name: "endwin", 
    6171        args: [], 
    6272        result: 'sint32}); 
     
    111121    |libcurses.endwin|(); 
    112122 
     123    // call open(1) 
     124    local popenref := |libc.popen|("open /Applications/TextEdit.app/", "r"); 
     125    |libc.pclose|(popenref); 
     126 
    113127    libm:Close(); 
    114128    libc:Close();