| 1 | /* |
| 2 | * Copyright 2008-2018 Aerospike, Inc. |
| 3 | * |
| 4 | * Portions may be licensed to Aerospike, Inc. under one or more contributor |
| 5 | * license agreements. |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 8 | * use this file except in compliance with the License. You may obtain a copy of |
| 9 | * the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | * License for the specific language governing permissions and limitations under |
| 15 | * the License. |
| 16 | */ |
| 17 | |
| 18 | #include <aerospike/as_val.h> |
| 19 | |
| 20 | #include <aerospike/mod_lua_reg.h> |
| 21 | |
| 22 | #include "internal.h" |
| 23 | |
| 24 | int mod_lua_reg_object(lua_State * l, const char * name, const luaL_reg * table, const luaL_reg * metatable) { |
| 25 | |
| 26 | int tableId = 0, metatableId = 0; |
| 27 | |
| 28 | luaL_register(l, name, table); |
| 29 | tableId = lua_gettop(l); |
| 30 | |
| 31 | lua_newtable(l); |
| 32 | luaL_register(l, 0, metatable); |
| 33 | metatableId = lua_gettop(l); |
| 34 | |
| 35 | lua_pushvalue(l, tableId); |
| 36 | lua_pushvalue(l, metatableId); |
| 37 | lua_setmetatable(l, 0); |
| 38 | |
| 39 | lua_pushliteral(l, "__metatable" ); |
| 40 | lua_pushvalue(l, tableId); |
| 41 | lua_rawset(l, metatableId); |
| 42 | |
| 43 | |
| 44 | lua_pop(l, 1); |
| 45 | |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | int mod_lua_reg_class(lua_State * l, const char * name, const luaL_reg * table, const luaL_reg * metatable) { |
| 50 | |
| 51 | int tableId = 0, metatableId = 0; |
| 52 | |
| 53 | if ( table ) { |
| 54 | luaL_register(l, name, table); |
| 55 | tableId = lua_gettop(l); |
| 56 | } |
| 57 | |
| 58 | if ( metatable ) { |
| 59 | luaL_newmetatable(l, name); |
| 60 | luaL_register(l, 0, metatable); |
| 61 | metatableId = lua_gettop(l); |
| 62 | } |
| 63 | |
| 64 | if ( table && metatable ) { |
| 65 | lua_pushliteral(l, "__index" ); |
| 66 | lua_pushvalue(l, tableId); |
| 67 | lua_rawset(l, metatableId); |
| 68 | |
| 69 | lua_pushliteral(l, "__metatable" ); |
| 70 | lua_pushvalue(l, tableId); |
| 71 | lua_rawset(l, metatableId); |
| 72 | |
| 73 | lua_pop(l, 1); |
| 74 | } |
| 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |