summaryrefslogtreecommitdiff
path: root/core/src/xmake/prefix.h
blob: f1287b5c9a9b00dec38461083fb987c4699dc67b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*!A cross-platform build utility based on Lua
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Copyright (C) 2015-present, TBOOX Open Source Group.
 *
 * @author      ruki
 * @file        prefix.h
 *
 */
#ifndef XM_PREFIX_H
#define XM_PREFIX_H

/* //////////////////////////////////////////////////////////////////////////////////////
 * includes
 */
#include "prefix/prefix.h"
#include "luaconf.h"
#if defined(TB_CONFIG_OS_WINDOWS) && defined(__cplusplus)
#   undef LUA_API
#   undef LUALIB_API
#   define LUA_API extern "C"
#   define LUALIB_API	LUA_API
#endif
#include "luajit.h"
#include "lualib.h"
#include "lauxlib.h"

/* //////////////////////////////////////////////////////////////////////////////////////
 * private interfaces
 */

// this issue has been fixed, @see https://github.com/LuaJIT/LuaJIT/commit/e9af1abec542e6f9851ff2368e7f196b6382a44c
#if 0//TB_CPU_BIT64
/* we use this interface instead of lua_pushlightuserdata() to fix bad light userdata pointer bug
 *
 * @see https://github.com/xmake-io/xmake/issues/914
 * https://github.com/LuaJIT/LuaJIT/pull/230
 *
 * @note we cannot lua_newuserdata() because we need pass this pointer to the external lua code
 * in poller_wait()/event_callback, but lua_pushuserdata does not exists
 */
static __tb_inline__ tb_void_t xm_lua_pushpointer(lua_State* lua, tb_pointer_t ptr)
{
    tb_uint64_t ptrval = (tb_uint64_t)ptr;
    if ((ptrval >> 47) == 0)
        lua_pushlightuserdata(lua, ptr);
    else
    {
        tb_char_t str[64];
        tb_long_t len = tb_snprintf(str, sizeof(str), "%p", ptr);
        lua_pushlstring(lua, str, len);
    }
}
static __tb_inline__ tb_bool_t xm_lua_ispointer(lua_State* lua, tb_int_t idx)
{
    return lua_isuserdata(lua, idx) || lua_isstring(lua, idx);
}
static __tb_inline__ tb_pointer_t xm_lua_topointer2(lua_State* lua, tb_int_t idx, tb_char_t const** pstr)
{
    tb_pointer_t ptr = tb_null;
    if (lua_isuserdata(lua, idx))
    {
        ptr = lua_touserdata(lua, idx);
        if (pstr) *pstr = tb_null;
    }
    else
    {
        size_t len = 0;
        tb_char_t const* str = luaL_checklstring(lua, idx, &len);
        if (str && len > 2 && str[0] == '0' && str[1] == 'x')
            ptr = (tb_pointer_t)tb_s16tou64(str);
        if (pstr) *pstr = str;
    }
    return ptr;
}
static __tb_inline__ tb_pointer_t xm_lua_topointer(lua_State* lua, tb_int_t idx)
{
   return xm_lua_topointer2(lua, idx, tb_null);
}
#else
static __tb_inline__ tb_void_t xm_lua_pushpointer(lua_State* lua, tb_pointer_t ptr)
{
    lua_pushlightuserdata(lua, ptr);
}
static __tb_inline__ tb_bool_t xm_lua_ispointer(lua_State* lua, tb_int_t idx)
{
    return lua_isuserdata(lua, idx);
}
static __tb_inline__ tb_pointer_t xm_lua_topointer2(lua_State* lua, tb_int_t idx, tb_char_t const** pstr)
{
    if (pstr) *pstr = tb_null;
    return lua_touserdata(lua, idx);
}
static __tb_inline__ tb_pointer_t xm_lua_topointer(lua_State* lua, tb_int_t idx)
{
    return lua_touserdata(lua, idx);
}
#endif

#endif