Show
Ignore:
Timestamp:
05/26/06 19:14:30 (3 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)

Files:
1 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);