Changeset 619

Show
Ignore:
Timestamp:
08/01/08 10:24:51 (4 months ago)
Author:
weyrick
Message:

some semblance of function invocation through the runtime

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/rphp/runtime/include/pExtBase.h

    r618 r619  
    2121 
    2222#include <string> 
     23#include "pTypes.h" 
    2324#include "pFunctionManager.h" 
    2425 
     
    3334        pRuntimeEngine* runtime; 
    3435 
    35         void registerBuiltin(std::string name, pFunPointer1 f); 
     36        void registerBuiltin(pUString name, pFunPointer1 f); 
    3637 
    3738    public: 
  • trunk/rphp/runtime/include/pExtManager.h

    r617 r619  
    3030    class pExtManager { 
    3131 
    32         typedef std::deque<pExtBase*> registryType; 
     32        typedef std::deque<pExtBase*> extRegistryType; 
    3333 
    3434        private: 
    3535            pRuntimeEngine* runtime; 
    36             registryType extRegistry; 
     36            extRegistryType extRegistry; 
    3737 
    3838        public: 
    39             pExtManager(pRuntimeEngine *r); 
     39            pExtManager(pRuntimeEngine *r): runtime(r) { } 
    4040            ~pExtManager(); 
     41 
     42            void startUp(); 
    4143 
    4244    }; 
  • trunk/rphp/runtime/include/pFunctionManager.h

    r618 r619  
    2121 
    2222#include <string> 
     23#include <boost/multi_index_container.hpp> 
     24#include <boost/multi_index/hashed_index.hpp> 
     25#include <boost/multi_index/member.hpp> 
     26#include <ext/hash_map> 
    2327#include "pFunctionSig.h" 
     28 
     29using boost::multi_index_container; 
     30using namespace boost::multi_index; 
    2431 
    2532namespace rphp { 
    2633 
     34    struct functionEntry { 
     35        pUString canonicalName; 
     36        pFunctionSig* signature; 
     37        functionEntry(pUString name, pFunctionSig* sig) : canonicalName(name.toLower()), signature(sig) { } 
     38    }; 
     39 
     40    typedef multi_index_container< 
     41      functionEntry, 
     42      indexed_by< 
     43       hashed_unique < 
     44        BOOST_MULTI_INDEX_MEMBER(functionEntry, const pUString, canonicalName) 
     45       > 
     46      > 
     47    > functionRegistryType; 
     48 
     49    class pRuntimeEngine; 
     50 
    2751    class pFunctionManager { 
    2852 
    29  
    3053        private: 
     54            pRuntimeEngine* runtime; 
     55            functionRegistryType functionRegistry; 
    3156 
    3257        public: 
    3358 
    34             //pFunctionManager(pRuntimeEngine *r) : runtime(r) { } 
     59            pFunctionManager(pRuntimeEngine *r) : runtime(r) { } 
     60            ~pFunctionManager() { 
     61                // TODO delete function signatures 
     62            } 
    3563 
    36             void registerBuiltin(std::string name, pFunPointer1 f); 
     64            void registerBuiltin(const pExtBase*, const pUString&, const pFunPointer1&); 
     65 
     66            pVar invoke(pUString funName, pVar arg1) { 
     67                functionRegistryType::iterator function = functionRegistry.find(funName.toLower()); 
     68                if (function != functionRegistry.end()) { 
     69                    return (*function).signature->invoke(arg1); 
     70                } 
     71                else { 
     72                    return pNull; 
     73                } 
     74            } 
    3775 
    3876    }; 
  • trunk/rphp/runtime/include/pFunctionSig.h

    r618 r619  
    2121 
    2222#include <vector> 
    23 #include <string> 
    2423#include <boost/function.hpp> 
    25 #include "pVar.h" 
     24#include "pTypes.h" 
    2625 
    2726namespace rphp { 
     
    4544    class pFunctionSig { 
    4645        private: 
    47             // declaration location 
    48             pUInt startLineNum
    49             pUInt endLineNum; 
    50             pUString fileName; 
    51             pExtBase* parentExtension; 
     46            // declaration location (user function) 
     47            const pSourceStartEndLocation sourceLocation
     48 
     49            // or parent extension (only builtins) 
     50            const pExtBase* parentExtension; 
    5251 
    5352            // docComment? 
    5453 
    5554            // signature 
    56             pUString functionName; 
    57             pUString canonicalName; 
    58             pFunType funType; 
    59             pUInt arity; 
    60             pUInt minRequiredArity; 
    61             bool isVarArity; 
     55            const pUString functionName; 
     56            const pFunType funType; 
     57            const pUInt requiredArity; 
     58            const pUInt maxArity; 
     59            const bool isVarArity; 
    6260 
    6361            std::vector<pFunctionParam> paramList; 
    6462 
     63            const pFunPointer1 funPointer1; 
     64 
    6565        public: 
     66 
     67            // standard builtin function: one argument 
     68            pFunctionSig(const pExtBase* e, const pUString& f, const pFunPointer1& fun) : 
     69                parentExtension(e), 
     70                functionName(f), 
     71                funType(pBuiltinFunType), 
     72                requiredArity(1), 
     73                maxArity(1), 
     74                isVarArity(false), 
     75                funPointer1(fun) 
     76            { 
     77 
     78            } 
     79 
     80            // invocation 
     81            pVar invoke(pVar arg1) { 
     82                return funPointer1(arg1); 
     83            } 
    6684 
    6785    }; 
  • trunk/rphp/runtime/include/pTypes.h

    r618 r619  
    2121 
    2222#include <boost/lexical_cast.hpp> 
     23#include <boost/tuple/tuple.hpp> 
    2324 
    2425#include "pVar.h" 
     
    2627#include "pObject.h" 
    2728 
     29U_NAMESPACE_BEGIN 
     30    // boost hash function for pUString 
     31    std::size_t hash_value(rphp::pUString const& k); 
     32U_NAMESPACE_END 
     33 
    2834namespace rphp { 
     35 
     36    // types used in the runtime which aren't related to pVar (i.e. that aren't builtin php types) 
     37 
     38    // note, pUInt is not a base PHP type (all PHP numbers are signed) 
     39    typedef unsigned long pUInt; 
     40 
     41    // source locations: filename/linenum 
     42    typedef boost::tuple<const pUString, const pUInt> pSourceLocation; 
     43 
     44    // source locations: filename/startlinenum/endlinenum 
     45    typedef boost::tuple<const pUString, const pUInt, const pUInt> pSourceStartEndLocation; 
    2946 
    3047    // a visitor for determining type of pVar 
     
    186203    } 
    187204 
    188     inline long pVar_getVal_pInt(const pVar &p) { 
     205    inline pInt pVar_getVal_pInt(const pVar &p) { 
    189206            return boost::get<pInt>(p); 
    190207    } 
  • trunk/rphp/runtime/include/pVar.h

    r618 r619  
    4747    typedef double pFloat; 
    4848 
    49     // note, pUInt is not a base PHP type (all PHP numbers are signed) 
    50     typedef unsigned long pUInt; 
    51  
    5249    // string types: binary and unicode flavor 
    5350    // "binary" strings 
  • trunk/rphp/runtime/pExtBase.cpp

    r618 r619  
    2222namespace rphp { 
    2323 
    24 void pExtBase::registerBuiltin(std::string name, pFunPointer1 f) { 
     24void pExtBase::registerBuiltin(pUString name, pFunPointer1 f) { 
    2525 
    26     runtime->functionManager->registerBuiltin(name, f); 
     26    runtime->functionManager->registerBuiltin(this, name, f); 
    2727 
    2828} 
  • trunk/rphp/runtime/pExtManager.cpp

    r617 r619  
    2424namespace rphp { 
    2525 
     26/* 
    2627pExtManager::pExtManager(pRuntimeEngine *r) : runtime(r) { 
    2728 
     29 
     30} 
     31*/ 
     32 
     33void pExtManager::startUp() { 
     34 
    2835    // initialize standard library 
    29     pStandardExt* sext = new pStandardExt(r); 
     36    pStandardExt* sext = new pStandardExt(runtime); 
    3037    sext->extensionStartup(); 
    3138    extRegistry.push_back(sext); 
     
    3643 
    3744    // shutdown extensions 
    38     for (registryType::iterator i=extRegistry.begin(); i!=extRegistry.end(); ++i) { 
     45    for (extRegistryType::iterator i=extRegistry.begin(); i!=extRegistry.end(); ++i) { 
    3946        (*i)->extensionShutdown(); 
    4047        delete *i; 
  • trunk/rphp/runtime/pFunctionManager.cpp

    r618 r619  
    2222namespace rphp { 
    2323 
    24 void pFunctionManager::registerBuiltin(std::string name, pFunPointer1 f) { 
     24void pFunctionManager::registerBuiltin(const pExtBase* sourceExt, const pUString& funName, const pFunPointer1& f) { 
    2525 
    26     std::cout << "registering " << name << std::endl; 
     26    std::cout << "registering " << funName << std::endl; 
     27    functionRegistry.insert(functionEntry(funName, new pFunctionSig(sourceExt, funName, f))); 
    2728 
    2829} 
  • trunk/rphp/runtime/pRuntime.cpp

    r618 r619  
    2525 
    2626pRuntimeEngine::pRuntimeEngine() : extManager(new pExtManager(this)), 
    27                                    functionManager(new pFunctionManager()) 
     27                                   functionManager(new pFunctionManager(this)) 
    2828{ 
    2929    // runtime initialization 
    30  
    31     // load standard extension 
     30    extManager->startUp(); 
    3231 
    3332} 
  • trunk/rphp/runtime/pTypes.cpp

    r618 r619  
    1818 
    1919#include "pTypes.h" 
     20 
     21U_NAMESPACE_BEGIN 
     22    std::size_t hash_value(rphp::pUString const& k) { 
     23        return (std::size_t)k.hashCode(); 
     24    } 
     25U_NAMESPACE_END 
    2026 
    2127namespace rphp { 
  • trunk/rphp/runtime/test/CMakeLists.txt

    r611 r619  
    77 
    88# XXX fixme 
    9 link_directories (${CMAKE_SOURCE_DIR}/../build/) 
     9link_directories (${CMAKE_SOURCE_DIR}/../../build/runtime/) 
    1010include_directories (${CMAKE_SOURCE_DIR} ${CMAKE_SOURCE_DIR}/../include/) 
    1111 
    1212# test apps 
    13 add_executable( rphp-runtime-suite main.cpp phashTestCase.cpp pvarTestCase.cpp
     13add_executable( rphp-runtime-suite main.cpp phashTestCase.cpp pvarTestCase.cpp functionTestCase.cpp
    1414target_link_libraries( rphp-runtime-suite cppunit rphp-runtime ${ICU_LIBRARIES} ${ICU_IO_LIBRARIES})