Coverage Report

Created: 2026-06-28 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/moddable/xs/sources/xsAll.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2016-2017  Moddable Tech, Inc.
3
 *
4
 *   This file is part of the Moddable SDK Runtime.
5
 * 
6
 *   The Moddable SDK Runtime is free software: you can redistribute it and/or modify
7
 *   it under the terms of the GNU Lesser General Public License as published by
8
 *   the Free Software Foundation, either version 3 of the License, or
9
 *   (at your option) any later version.
10
 * 
11
 *   The Moddable SDK Runtime is distributed in the hope that it will be useful,
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *   GNU Lesser General Public License for more details.
15
 * 
16
 *   You should have received a copy of the GNU Lesser General Public License
17
 *   along with the Moddable SDK Runtime.  If not, see <http://www.gnu.org/licenses/>.
18
 *
19
 * This file incorporates work covered by the following copyright and  
20
 * permission notice:  
21
 *
22
 *       Copyright (C) 2010-2016 Marvell International Ltd.
23
 *       Copyright (C) 2002-2010 Kinoma, Inc.
24
 *
25
 *       Licensed under the Apache License, Version 2.0 (the "License");
26
 *       you may not use this file except in compliance with the License.
27
 *       You may obtain a copy of the License at
28
 *
29
 *        http://www.apache.org/licenses/LICENSE-2.0
30
 *
31
 *       Unless required by applicable law or agreed to in writing, software
32
 *       distributed under the License is distributed on an "AS IS" BASIS,
33
 *       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34
 *       See the License for the specific language governing permissions and
35
 *       limitations under the License.
36
 */
37
38
#define _GNU_SOURCE
39
#include "xsAll.h"
40
#if mxMacOSX || mxLinux
41
#include <dlfcn.h>
42
#endif
43
44
static void fxBufferFunctionNameAddress(txMachine* the, txString buffer, txSize size, txID id, txCallback address, txID profileID);
45
static void fxBufferName(txString dst, txString src, txSize size);
46
47
txString fxAdornStringC(txMachine* the, txString prefix, txSlot* string, txString suffix)
48
472k
{
49
472k
  txSize stringSize = mxStringLength(string->value.string);
50
472k
  txSize prefixSize = prefix ? mxStringLength(prefix) : 0;
51
472k
  txSize suffixSize = suffix ? mxStringLength(suffix) : 0;
52
472k
  txSize resultSize = fxAddChunkSizes(the, fxAddChunkSizes(the, fxAddChunkSizes(the, stringSize, prefixSize), suffixSize), 1);
53
472k
  txString result = (txString)fxNewChunk(the, resultSize);
54
472k
  if (prefix && prefixSize)
55
472k
    c_memcpy(result, prefix, prefixSize);
56
472k
  if (stringSize)
57
472k
    c_memcpy(result + prefixSize, string->value.string, stringSize);
58
472k
  if (suffix && suffixSize)
59
149k
    c_memcpy(result + prefixSize + stringSize, suffix, suffixSize);
60
472k
  result[prefixSize + stringSize + suffixSize] = 0;
61
472k
  string->kind = XS_STRING_KIND;
62
472k
  string->value.string = result;
63
472k
  return result;
64
472k
}
65
66
txSlot* fxArgToCallback(txMachine* the, txInteger argi)
67
0
{
68
0
  if (mxArgc > argi) {
69
0
    txSlot* slot = mxArgv(argi);
70
0
    if (slot->kind == XS_REFERENCE_KIND) {
71
0
      txSlot* instance = slot->value.reference;
72
0
again:
73
0
      if (instance) {
74
0
        txSlot* exotic = instance->next;
75
0
        if (exotic && (exotic->flag & XS_INTERNAL_FLAG)) {
76
0
          if (((exotic->kind == XS_CALLBACK_KIND) || (exotic->kind == XS_CALLBACK_X_KIND) || (exotic->kind == XS_CODE_KIND) || (exotic->kind == XS_CODE_X_KIND)))
77
0
            return slot;
78
0
          if (exotic->kind == XS_PROXY_KIND) {
79
0
            instance = exotic->value.proxy.target;
80
0
            goto again;
81
0
          }
82
0
        }
83
0
      }
84
0
    }
85
#if mxHostFunctionPrimitive
86
    if (slot->kind == XS_HOST_FUNCTION_KIND)
87
      return slot;
88
#endif
89
0
  }
90
0
  mxTypeError("callback: not a function");
91
0
  return C_NULL;
92
0
}
93
94
void fxBufferFrameName(txMachine* the, txString buffer, txSize size, txSlot* frame, txString suffix)
95
4.67k
{
96
4.67k
  txSlot* function = frame + 2;
97
4.67k
  txSlot* _this = frame + 3;
98
4.67k
  if (function->kind == XS_REFERENCE_KIND) {
99
4.67k
    function = function->value.reference;
100
4.67k
    if (mxIsFunction(function)) {
101
4.67k
      if (!(frame->flag & XS_TARGET_FLAG)) {
102
4.67k
        txSlot* home = mxFunctionInstanceHome(function)->value.home.object;
103
4.67k
        if (home) {
104
4.67k
          if (mxIsFunction(home)) {
105
0
            fxBufferFunctionName(the, buffer, size, home, ".");
106
0
          }
107
4.67k
          else {
108
4.67k
            txSlot* constructor = mxBehaviorGetProperty(the, home, mxID(_constructor), 0, XS_OWN);
109
4.67k
            if (constructor) {
110
0
              if (constructor->kind == XS_REFERENCE_KIND) {
111
0
                constructor = constructor->value.reference;
112
0
                if (mxIsFunction(constructor))
113
0
                  fxBufferFunctionName(the, buffer, size, constructor, ".prototype.");
114
0
              }
115
0
            }
116
4.67k
            else if (_this->kind == XS_REFERENCE_KIND) {
117
4.67k
              fxBufferObjectName(the, buffer, size, _this->value.reference, ".");
118
4.67k
            }
119
4.67k
          }
120
4.67k
        }
121
4.67k
      }
122
4.67k
      fxBufferFunctionName(the, buffer, size, function, "");
123
4.67k
    }
124
4.67k
  }
125
#if mxHostFunctionPrimitive
126
  else if (function->kind == XS_HOST_FUNCTION_KIND) {
127
    fxBufferFunctionNameAddress(the, buffer, size, function->value.hostFunction.builder->id, function->value.hostFunction.builder->callback, function->value.hostFunction.profileID);
128
  }
129
#endif
130
0
  else
131
0
    c_strncat(buffer, "(host)", size - mxStringLength(buffer) - 1);
132
4.67k
  c_strncat(buffer, suffix, size - mxStringLength(buffer) - 1);
133
4.67k
}
134
135
void fxBufferFunctionName(txMachine* the, txString buffer, txSize size, txSlot* function, txString suffix)
136
4.67k
{
137
4.67k
  txSlot* slot = mxFunctionInstanceCode(function);
138
4.67k
  txSlot* home = mxFunctionInstanceHome(function);
139
4.67k
  if ((slot->kind == XS_CODE_KIND) || (slot->kind == XS_CODE_X_KIND))
140
0
    fxBufferFunctionNameAddress(the, buffer, size, slot->ID, C_NULL, home->ID);
141
4.67k
  else
142
4.67k
    fxBufferFunctionNameAddress(the, buffer, size, slot->ID, slot->value.callback.address, home->ID);
143
4.67k
    c_strncat(buffer, suffix, size - mxStringLength(buffer) - 1);
144
4.67k
}
145
146
void fxBufferFunctionNameAddress(txMachine* the, txString buffer, txSize size, txID id, txCallback address, txID profileID)
147
4.67k
{
148
4.67k
  txInteger length;
149
4.67k
  if (id != XS_NO_ID) {
150
4.67k
    txBoolean adorn;
151
4.67k
    txString string = fxGetKeyString(the, id, &adorn);
152
4.67k
    if (adorn)
153
0
      c_strncat(buffer, "[", size - mxStringLength(buffer) - 1);
154
4.67k
    fxBufferName(buffer, string, size - mxStringLength(buffer) - 1);
155
4.67k
    if (adorn)
156
0
      c_strncat(buffer, "]", size - mxStringLength(buffer) - 1);
157
4.67k
    return;
158
4.67k
  }
159
0
  if (address) {
160
0
    c_strncat(buffer, "@", size - mxStringLength(buffer) - 1);
161
0
#if mxMacOSX || mxLinux
162
0
    Dl_info info;
163
0
    if (dladdr(address, &info) && info.dli_sname)
164
0
      c_strncat(buffer, info.dli_sname, size - mxStringLength(buffer) - 1);
165
0
    else 
166
0
#endif
167
0
    {
168
0
      c_strncat(buffer, "anonymous-", size - mxStringLength(buffer) - 1);
169
0
      length = mxStringLength(buffer);
170
0
      fxIntegerToString(the, profileID, buffer + length, size - length - 1);
171
0
    }
172
0
  }
173
0
  else {
174
0
    c_strncat(buffer, "(anonymous-", size - mxStringLength(buffer) - 1);
175
0
    length = mxStringLength(buffer);
176
0
    fxIntegerToString(the, profileID, buffer + length, size - length - 1);
177
0
    c_strncat(buffer, ")", size - mxStringLength(buffer) - 1);
178
0
  }
179
0
}
180
181
void fxBufferObjectName(txMachine* the, txString buffer, txSize size, txSlot* object, txString suffix)
182
4.67k
{
183
4.67k
  txSlot* slot = mxBehaviorGetProperty(the, object, mxID(_Symbol_toStringTag), 0, XS_ANY);
184
4.67k
  if (slot && ((slot->kind == XS_STRING_KIND) || (slot->kind == XS_STRING_X_KIND)) && !c_isEmpty(slot->value.string)) {
185
4.67k
    fxBufferName(buffer, slot->value.string, size - mxStringLength(buffer) - 1);
186
4.67k
    c_strncat(buffer, suffix, size - mxStringLength(buffer) - 1);
187
4.67k
  }
188
4.67k
}
189
190
void fxBufferName(txString dst, txString src, txSize size)
191
9.34k
{
192
9.34k
  dst += mxStringLength(dst);
193
51.4k
  for (;;) {
194
51.4k
    txInteger c;
195
51.4k
    txSize length;
196
51.4k
    src = mxStringByteDecode(src, &c);
197
51.4k
    if (c == C_EOF)
198
9.34k
      break;
199
42.0k
    length = mxStringByteLength(c);
200
42.0k
    if (length > size)
201
0
      break;
202
42.0k
    dst = mxStringByteEncode(dst, c);
203
42.0k
    size -= length;
204
42.0k
  }
205
9.34k
  *dst = 0;
206
9.34k
}
207
208
txString fxConcatString(txMachine* the, txSlot* a, txSlot* b)
209
0
{
210
0
  txSize aSize = mxStringLength(a->value.string);
211
0
  txSize bSize = mxStringLength(b->value.string);
212
0
  txSize resultSize = fxAddChunkSizes(the, fxAddChunkSizes(the, aSize, bSize), 1);
213
0
  txString result = (txString)fxNewChunk(the, resultSize);
214
0
  c_memcpy(result, a->value.string, aSize);
215
0
  c_memcpy(result + aSize, b->value.string, bSize + 1);
216
0
  a->value.string = result;
217
0
  a->kind = XS_STRING_KIND;
218
0
  return result;
219
0
}
220
221
txString fxConcatStringC(txMachine* the, txSlot* a, txString b)
222
0
{
223
0
  txSize aSize = mxStringLength(a->value.string);
224
0
  txSize bSize = mxStringLength(b);
225
0
  txSize resultSize = fxAddChunkSizes(the, fxAddChunkSizes(the, aSize, bSize), 1);
226
0
  txString result = C_NULL;
227
0
  if (a->kind == XS_STRING_KIND)
228
0
    result = (txString)fxRenewChunk(the, a->value.string, resultSize);
229
0
  if (!result) {
230
0
    result = (txString)fxNewChunk(the, resultSize);
231
0
    c_memcpy(result, a->value.string, aSize);
232
0
    a->value.string = result;
233
0
    a->kind = XS_STRING_KIND;
234
0
  }
235
0
  c_memcpy(result + aSize, b, bSize + 1);
236
0
  return result;
237
0
}
238
239
txString fxCopyString(txMachine* the, txSlot* a, txSlot* b)
240
0
{
241
0
  txString result = b->value.string;
242
0
  a->value.string = result;
243
0
  a->kind = b->kind;
244
0
  return result;
245
0
}
246
247
txString fxCopyStringC(txMachine* the, txSlot* a, txString b)
248
489k
{
249
489k
  txSize bSize = mxStringLength(b);
250
489k
  txSize resultSize = fxAddChunkSizes(the, bSize, 1);
251
489k
  txString result = (txString)fxNewChunk(the, resultSize);
252
489k
  c_memcpy(result, b, resultSize);
253
489k
  a->value.string = result;
254
489k
  a->kind = XS_STRING_KIND;
255
489k
  return result;
256
489k
}
257
258
txBoolean fxIsCanonicalIndex(txMachine* the, txID id)
259
0
{
260
0
  txSlot* key = fxGetKey(the, id);
261
0
  if (key->flag & XS_DONT_ENUM_FLAG) {
262
0
    txString string = key->value.key.string;
263
0
    char buffer[256], c;
264
0
    txNumber number;
265
0
    c = c_read8(string);
266
0
    if (('+' != c) && ('-' != c) && ('.' != c) && ('I' != c) && ('N' != c) && !(('0' <= c) && ('9' >= c)))
267
0
      return 0;
268
0
    number = fxStringToNumber(the, string, 1);
269
0
    if (number == -0)
270
0
      return 1;
271
0
    fxNumberToString(the, number, buffer, sizeof(buffer), 0, 0);
272
0
    if (!c_strcmp(string, buffer)) {
273
0
      return 1;
274
0
    }
275
0
  }
276
0
  return 0;
277
0
}
278
279
int fxStringGetter(void* theStream)
280
0
{
281
0
  txStringStream* aStream = (txStringStream*)theStream;
282
0
  int result = C_EOF;
283
  
284
0
  if (aStream->offset < aStream->size) {
285
0
    result = *(aStream->slot->value.string + aStream->offset);
286
0
    aStream->offset++;
287
0
  }
288
0
  return result;
289
0
}
290
291
int fxStringCGetter(void* theStream)
292
0
{
293
0
  txStringCStream* aStream = (txStringCStream*)theStream;
294
0
  int result = C_EOF;
295
  
296
0
  if (aStream->offset < aStream->size) {
297
0
    result = *(aStream->buffer + aStream->offset);
298
0
    aStream->offset++;
299
0
  }
300
0
  return result;
301
0
}
302
303
void fxJump(txMachine* the)
304
4.67k
{
305
4.67k
  txJump* aJump = the->firstJump;
306
4.67k
  c_longjmp(aJump->buffer, 1);
307
4.67k
}