Changeset 358


Ignore:
Timestamp:
04/29/15 14:53:06 (10 years ago)
Author:
epyon
Message:
  • fixed string_ref constructor
  • implementing string_ref usage in lua: lua_nova string usage optimized lua_values string_ref entries
Location:
trunk
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/nv/core/string_ref.hh

    r357 r358  
    6060                template< typename U, typename std::enable_if<std::is_same<U,const char*>::value>::type* = nullptr >
    6161                basic_string_ref( U str )
    62                         : m_data( str ), m_length( TRAITS::length( str ) )
    63                 {
    64                         static_assert( std::is_same<T, U>::value  );
    65                 }
     62                        : m_data( str ), m_length( TRAITS::length( str ) ) {}
    6663
    6764                basic_string_ref& operator=( const basic_string_ref &rhs )
  • trunk/nv/lua/lua_function.hh

    r345 r358  
    99
    1010#include <nv/core/common.hh>
    11 #include <nv/core/string.hh>
    1211#include <nv/lua/lua_values.hh>
    1312#include <nv/lua/lua_path.hh>
  • trunk/nv/lua/lua_nova.hh

    r319 r358  
    1313        namespace lua
    1414        {
    15                 void register_storage( state* a_state, const string& name, const string& constructor_name );
     15                void register_storage( state* a_state, string_ref name, string_ref constructor_name );
    1616                void register_nova( state* a_state );
    1717        }
  • trunk/nv/lua/lua_state.hh

    r345 r358  
    1515#include <nv/core/flags.hh>
    1616#include <nv/core/handle.hh>
     17#include <nv/core/string_ref.hh>
    1718#include <istream>
    1819#include <map>
  • trunk/nv/lua/lua_values.hh

    r349 r358  
    1111#include <nv/core/type_traits.hh>
    1212#include <nv/core/string.hh>
     13#include <nv/core/string_ref.hh>
    1314
    1415struct lua_State;
     
    7475                        void push_bool       ( lua_State *L, bool v );
    7576                        void push_string     ( lua_State *L, const std::string& s );
     77                        void push_string_ref ( lua_State *L, string_ref s );
    7678                        void push_cstring    ( lua_State *L, const char* s );
    7779                        void push_pointer    ( lua_State *L, void* p );
     
    8486                        std::string to_string     ( lua_State *L, int index );
    8587                        const char* to_cstring    ( lua_State *L, int index );
     88                        string_ref  to_string_ref ( lua_State *L, int index );
    8689                        void*       to_pointer    ( lua_State *L, int index );
    8790                        void*       to_ref_object ( lua_State *L, int index );
     
    9396                        std::string to_string     ( lua_State *L, int index, const std::string& def );
    9497                        const char* to_cstring    ( lua_State *L, int index, const char* def );
     98                        string_ref  to_string_ref ( lua_State *L, int index, string_ref def );
    9599                        void*       to_pointer    ( lua_State *L, int index, void* def );
    96100                        void*       to_ref_object ( lua_State *L, int index, void* def );
     
    156160
    157161                template <>
     162                struct pass_traits < string_ref >
     163                {
     164                        static void push( lua_State *L, string_ref s ) { detail::push_string_ref( L, s ); }
     165                        static string_ref to( lua_State *L, int index ) { return detail::to_string_ref( L, index ); }
     166                        static string_ref to( lua_State *L, int index, string_ref def ) { return detail::to_string_ref( L, index, def ); }
     167                };
     168
     169                template <>
    158170                struct pass_traits<ref>
    159171                {
  • trunk/src/engine/resource_system.cc

    r323 r358  
    1414        std::string constructor_name( get_resource_name() );
    1515        constructor_name = "register_"+constructor_name;
    16         lua::register_storage( m_lua, get_storage_name(), constructor_name.c_str() );
     16        int correct = 0;
     17        lua::register_storage( m_lua, get_storage_name(), constructor_name );
    1718}
    1819
  • trunk/src/lua/lua_nova.cc

    r323 r358  
    8383                lua_pushvalue( L, itype );
    8484                lua_pushvalue( L, iid );
    85                 lua_pushstring( L, "." );
     85                lua_pushliteral( L, "." );
    8686                lua_pushvalue( L, ifield );
    8787                lua_concat( L, 3 );
     
    292292{
    293293        // storage.__counter++
    294         lua_pushstring( L, "__counter" );
     294        lua_pushliteral( L, "__counter" );
    295295        lua_pushvalue( L, -1 );
    296296        lua_rawget( L, 1 );
     
    303303
    304304        // element.nid = __counter
    305         lua_pushstring( L, "nid" );
     305        lua_pushliteral( L, "nid" );
    306306        lua_pushinteger( L, count );
    307307        lua_rawset( L, 2 );
     
    322322
    323323        // element.id = element.id
    324         lua_pushstring( L, "id" );
     324        lua_pushliteral( L, "id" );
    325325        lua_rawget( L, 2 );
    326326        if ( lua_isnil( L, -1 ) )
     
    413413
    414414        lua_pushvalue( L, 1 );
    415         lua_pushstring( L, "." );
     415        lua_pushliteral( L, "." );
    416416        lua_pushvalue( L, 2 );
    417417        lua_concat( L, 3 ); // new ident index 5
     
    449449
    450450        lua_pushvalue( L, 1 );
    451         lua_pushstring( L, "." );
     451        lua_pushliteral( L, "." );
    452452        lua_pushvalue( L, 2 );
    453453        lua_concat( L, 3 ); // new ident index 6
     
    579579        if ( lua_type( L, 1 ) == LUA_TSTRING )
    580580        {
     581                // TODO: Optimzie
    581582                lua_getglobal( L, lua_tostring( L, 1 ) );
    582583                lua_replace( L, 1 );
     
    809810}
    810811
    811 void nv::lua::register_storage( state* a_state, const string& name, const string& constructor_name )
     812void nv::lua::register_storage( state* a_state, string_ref name, string_ref constructor_name )
    812813{
    813814        // TODO: error checking
     
    816817        // TODO: check if nova is loaded
    817818        lua_pushcfunction( L, nova_register_storage );
    818         lua_pushstring( L, name.c_str() );
     819        lua_pushlstring( L, name.data(), name.size() );
    819820        lua_call( L, 1, 1 );
    820         lua_setglobal( L, constructor_name.c_str() );
     821        lua_setglobal( L, constructor_name.data() );
    821822        lua_settop( L, stack );
    822823}
  • trunk/src/lua/lua_values.cc

    r319 r358  
    7575}
    7676
     77void nv::lua::detail::push_string_ref( lua_State *L, string_ref s )
     78{
     79        lua_pushlstring( L, s.data(), s.size() );
     80}
     81
    7782void nv::lua::detail::push_pointer ( lua_State *L, void* p )
    7883{
     
    108113std::string nv::lua::detail::to_string  ( lua_State *L, int index )
    109114{
    110         return lua_tostring( L, index );
     115        size_t length = 0;
     116        const char* result = lua_tolstring( L, index, &length );
     117        return std::string( result, length );
    111118}
    112119
    113120const char* nv::lua::detail::to_cstring ( lua_State *L, int index )
    114121{
    115         return lua_tostring( L, index );
     122        return lua_tolstring( L, index, nullptr );
     123}
     124
     125nv::string_ref nv::lua::detail::to_string_ref( lua_State *L, int index )
     126{
     127        size_t length = 0;
     128        const char* result = lua_tolstring( L, index, &length );
     129        return string_ref( result, length );
    116130}
    117131
Note: See TracChangeset for help on using the changeset viewer.