~ubuntu-branches/ubuntu/trusty/luajit/trusty

« back to all changes in this revision

Viewing changes to src/lj_cdata.c

  • Committer: Bazaar Package Importer
  • Author(s): Enrico Tassi
  • Date: 2011-05-09 23:14:21 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20110509231421-zdcnqbqk5h6iryxr
Tags: 2.0.0~beta7+dfsg-1
New upstream release with arm support

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
#include "lj_gc.h"
11
11
#include "lj_err.h"
12
12
#include "lj_str.h"
 
13
#include "lj_tab.h"
13
14
#include "lj_ctype.h"
14
15
#include "lj_cconv.h"
15
16
#include "lj_cdata.h"
52
53
/* Free a C data object. */
53
54
void LJ_FASTCALL lj_cdata_free(global_State *g, GCcdata *cd)
54
55
{
55
 
  if (LJ_LIKELY(!cdataisv(cd))) {
 
56
  if (LJ_UNLIKELY(cd->marked & LJ_GC_CDATA_FIN)) {
 
57
    GCobj *root;
 
58
    cd->marked = curwhite(g) | LJ_GC_FINALIZED;
 
59
    if ((root = gcref(g->gc.mmudata)) != NULL) {
 
60
      setgcrefr(cd->nextgc, root->gch.nextgc);
 
61
      setgcref(root->gch.nextgc, obj2gco(cd));
 
62
      setgcref(g->gc.mmudata, obj2gco(cd));
 
63
    } else {
 
64
      setgcref(cd->nextgc, obj2gco(cd));
 
65
      setgcref(g->gc.mmudata, obj2gco(cd));
 
66
    }
 
67
  } else if (LJ_LIKELY(!cdataisv(cd))) {
56
68
    CType *ct = ctype_raw(ctype_ctsG(g), cd->typeid);
57
69
    CTSize sz = ctype_hassize(ct->info) ? ct->size : CTSIZE_PTR;
58
70
    lua_assert(ctype_hassize(ct->info) || ctype_isfunc(ct->info) ||
63
75
  }
64
76
}
65
77
 
 
78
TValue * LJ_FASTCALL lj_cdata_setfin(lua_State *L, GCcdata *cd)
 
79
{
 
80
  global_State *g = G(L);
 
81
  GCtab *t = ctype_ctsG(g)->finalizer;
 
82
  if (gcref(t->metatable)) {
 
83
    /* Add cdata to finalizer table, if still enabled. */
 
84
    TValue *tv, tmp;
 
85
    setcdataV(L, &tmp, cd);
 
86
    lj_gc_anybarriert(L, t);
 
87
    tv = lj_tab_set(L, t, &tmp);
 
88
    cd->marked |= LJ_GC_CDATA_FIN;
 
89
    return tv;
 
90
  } else {
 
91
    /* Otherwise return dummy TValue. */
 
92
    return &g->tmptv;
 
93
  }
 
94
}
 
95
 
66
96
/* -- C data indexing ----------------------------------------------------- */
67
97
 
68
98
/* Index C data by a TValue. Return CType and pointer. */
88
118
  }
89
119
  lua_assert(!ctype_isref(ct->info));  /* Interning rejects refs to refs. */
90
120
 
91
 
  if (tvisnum(key)) {  /* Numeric key. */
 
121
  if (tvisint(key)) {
 
122
    idx = (ptrdiff_t)intV(key);
 
123
    goto integer_key;
 
124
  } else if (tvisnum(key)) {  /* Numeric key. */
92
125
    idx = LJ_64 ? (ptrdiff_t)numV(key) : (ptrdiff_t)lj_num2int(numV(key));
93
126
  integer_key:
94
127
    if (ctype_ispointer(ct->info)) {
115
148
    }
116
149
  } else if (tvisstr(key)) {  /* String key. */
117
150
    GCstr *name = strV(key);
118
 
    if (ctype_isptr(ct->info)) {  /* Automatically perform '->'. */
119
 
      if (ctype_isstruct(ctype_rawchild(cts, ct)->info)) {
120
 
        p = (uint8_t *)cdata_getptr(p, ct->size);
121
 
        ct = ctype_child(cts, ct);
122
 
        goto collect_attrib;
123
 
      }
124
 
    } if (ctype_isstruct(ct->info)) {
 
151
    if (ctype_isstruct(ct->info)) {
125
152
      CTSize ofs;
126
153
      CType *fct = lj_ctype_getfield(cts, ct, name, &ofs);
127
154
      if (fct) {
141
168
      }
142
169
    } else if (cd->typeid == CTID_CTYPEID) {
143
170
      /* Allow indexing a (pointer to) struct constructor to get constants. */
144
 
      CType *sct = ct = ctype_raw(cts, *(CTypeID *)p);
 
171
      CType *sct = ctype_raw(cts, *(CTypeID *)p);
145
172
      if (ctype_isptr(sct->info))
146
173
        sct = ctype_rawchild(cts, sct);
147
174
      if (ctype_isstruct(sct->info)) {
151
178
          return fct;
152
179
      }
153
180
    }
154
 
    {
155
 
      GCstr *s = lj_ctype_repr(cts->L, ctype_typeid(cts, ct), NULL);
156
 
      lj_err_callerv(cts->L, LJ_ERR_FFI_BADMEMBER, strdata(s), strdata(name));
 
181
  }
 
182
  if (ctype_isptr(ct->info)) {  /* Automatically perform '->'. */
 
183
    if (ctype_isstruct(ctype_rawchild(cts, ct)->info)) {
 
184
      p = (uint8_t *)cdata_getptr(p, ct->size);
 
185
      ct = ctype_child(cts, ct);
 
186
      goto collect_attrib;
157
187
    }
158
188
  }
159
 
  {
160
 
    GCstr *s = lj_ctype_repr(cts->L, ctype_typeid(cts, ct), NULL);
161
 
    lj_err_callerv(cts->L, LJ_ERR_FFI_BADIDX, strdata(s));
162
 
  }
163
 
  return NULL;  /* unreachable */
 
189
  *qual |= 1;  /* Lookup failed. */
 
190
  return ct;  /* But return the resolved raw type. */
164
191
}
165
192
 
166
193
/* -- C data getters ------------------------------------------------------ */
171
198
  CType *ctt = ctype_child(cts, ct);
172
199
  lua_assert(ctype_isinteger(ctt->info) && ctt->size <= 4);
173
200
  /* Constants are already zero-extended/sign-extended to 32 bits. */
174
 
  if (!(ctt->info & CTF_UNSIGNED))
 
201
  if ((ctt->info & CTF_UNSIGNED) && (int32_t)ct->size < 0)
 
202
    setnumV(o, (lua_Number)(uint32_t)ct->size);
 
203
  else
175
204
    setintV(o, (int32_t)ct->size);
176
 
  else
177
 
    setnumV(o, (lua_Number)(uint32_t)ct->size);
178
205
}
179
206
 
180
207
/* Get C data value and convert to TValue. */