Coverage Report

Created: 2026-07-14 06:35

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
2.95M
{
49
2.95M
  txSize stringSize = mxStringLength(string->value.string);
50
2.95M
  txSize prefixSize = prefix ? mxStringLength(prefix) : 0;
51
2.95M
  txSize suffixSize = suffix ? mxStringLength(suffix) : 0;
52
2.95M
  txSize resultSize = fxAddChunkSizes(the, fxAddChunkSizes(the, fxAddChunkSizes(the, stringSize, prefixSize), suffixSize), 1);
53
2.95M
  txString result = (txString)fxNewChunk(the, resultSize);
54
2.95M
  if (prefix && prefixSize)
55
2.95M
    c_memcpy(result, prefix, prefixSize);
56
2.95M
  if (stringSize)
57
2.95M
    c_memcpy(result + prefixSize, string->value.string, stringSize);
58
2.95M
  if (suffix && suffixSize)
59
834k
    c_memcpy(result + prefixSize + stringSize, suffix, suffixSize);
60
2.95M
  result[prefixSize + stringSize + suffixSize] = 0;
61
2.95M
  string->kind = XS_STRING_KIND;
62
2.95M
  string->value.string = result;
63
2.95M
  return result;
64
2.95M
}
65
66
txSlot* fxArgToCallback(txMachine* the, txInteger argi)
67
332k
{
68
332k
  if (mxArgc > argi) {
69
332k
    txSlot* slot = mxArgv(argi);
70
332k
    if (slot->kind == XS_REFERENCE_KIND) {
71
332k
      txSlot* instance = slot->value.reference;
72
332k
again:
73
332k
      if (instance) {
74
332k
        txSlot* exotic = instance->next;
75
332k
        if (exotic && (exotic->flag & XS_INTERNAL_FLAG)) {
76
332k
          if (((exotic->kind == XS_CALLBACK_KIND) || (exotic->kind == XS_CALLBACK_X_KIND) || (exotic->kind == XS_CODE_KIND) || (exotic->kind == XS_CODE_X_KIND)))
77
332k
            return slot;
78
14
          if (exotic->kind == XS_PROXY_KIND) {
79
0
            instance = exotic->value.proxy.target;
80
0
            goto again;
81
0
          }
82
14
        }
83
332k
      }
84
332k
    }
85
#if mxHostFunctionPrimitive
86
    if (slot->kind == XS_HOST_FUNCTION_KIND)
87
      return slot;
88
#endif
89
332k
  }
90
332k
  mxTypeError("callback: not a function");
91
0
  return C_NULL;
92
332k
}
93
94
void fxBufferFrameName(txMachine* the, txString buffer, txSize size, txSlot* frame, txString suffix)
95
1.96M
{
96
1.96M
  txSlot* function = frame + 2;
97
1.96M
  txSlot* _this = frame + 3;
98
1.96M
  if (function->kind == XS_REFERENCE_KIND) {
99
1.96M
    function = function->value.reference;
100
1.96M
    if (mxIsFunction(function)) {
101
1.90M
      if (!(frame->flag & XS_TARGET_FLAG)) {
102
1.90M
        txSlot* home = mxFunctionInstanceHome(function)->value.home.object;
103
1.90M
        if (home) {
104
540k
          if (mxIsFunction(home)) {
105
129k
            fxBufferFunctionName(the, buffer, size, home, ".");
106
129k
          }
107
411k
          else {
108
411k
            txSlot* constructor = mxBehaviorGetProperty(the, home, mxID(_constructor), 0, XS_OWN);
109
411k
            if (constructor) {
110
248k
              if (constructor->kind == XS_REFERENCE_KIND) {
111
248k
                constructor = constructor->value.reference;
112
248k
                if (mxIsFunction(constructor))
113
248k
                  fxBufferFunctionName(the, buffer, size, constructor, ".prototype.");
114
248k
              }
115
248k
            }
116
162k
            else if (_this->kind == XS_REFERENCE_KIND) {
117
160k
              fxBufferObjectName(the, buffer, size, _this->value.reference, ".");
118
160k
            }
119
411k
          }
120
540k
        }
121
1.90M
      }
122
1.90M
      fxBufferFunctionName(the, buffer, size, function, "");
123
1.90M
    }
124
1.96M
  }
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
1.22k
  else
131
1.22k
    c_strncat(buffer, "(host)", size - mxStringLength(buffer) - 1);
132
1.96M
  c_strncat(buffer, suffix, size - mxStringLength(buffer) - 1);
133
1.96M
}
134
135
void fxBufferFunctionName(txMachine* the, txString buffer, txSize size, txSlot* function, txString suffix)
136
2.28M
{
137
2.28M
  txSlot* slot = mxFunctionInstanceCode(function);
138
2.28M
  txSlot* home = mxFunctionInstanceHome(function);
139
2.28M
  if ((slot->kind == XS_CODE_KIND) || (slot->kind == XS_CODE_X_KIND))
140
1.25M
    fxBufferFunctionNameAddress(the, buffer, size, slot->ID, C_NULL, home->ID);
141
1.02M
  else
142
1.02M
    fxBufferFunctionNameAddress(the, buffer, size, slot->ID, slot->value.callback.address, home->ID);
143
2.28M
    c_strncat(buffer, suffix, size - mxStringLength(buffer) - 1);
144
2.28M
}
145
146
void fxBufferFunctionNameAddress(txMachine* the, txString buffer, txSize size, txID id, txCallback address, txID profileID)
147
2.28M
{
148
2.28M
  txInteger length;
149
2.28M
  if (id != XS_NO_ID) {
150
1.14M
    txBoolean adorn;
151
1.14M
    txString string = fxGetKeyString(the, id, &adorn);
152
1.14M
    if (adorn)
153
185
      c_strncat(buffer, "[", size - mxStringLength(buffer) - 1);
154
1.14M
    fxBufferName(buffer, string, size - mxStringLength(buffer) - 1);
155
1.14M
    if (adorn)
156
185
      c_strncat(buffer, "]", size - mxStringLength(buffer) - 1);
157
1.14M
    return;
158
1.14M
  }
159
1.13M
  if (address) {
160
42.6k
    c_strncat(buffer, "@", size - mxStringLength(buffer) - 1);
161
42.6k
#if mxMacOSX || mxLinux
162
42.6k
    Dl_info info;
163
42.6k
    if (dladdr(address, &info) && info.dli_sname)
164
42.4k
      c_strncat(buffer, info.dli_sname, size - mxStringLength(buffer) - 1);
165
248
    else 
166
248
#endif
167
248
    {
168
248
      c_strncat(buffer, "anonymous-", size - mxStringLength(buffer) - 1);
169
248
      length = mxStringLength(buffer);
170
248
      fxIntegerToString(the, profileID, buffer + length, size - length - 1);
171
248
    }
172
42.6k
  }
173
1.08M
  else {
174
1.08M
    c_strncat(buffer, "(anonymous-", size - mxStringLength(buffer) - 1);
175
1.08M
    length = mxStringLength(buffer);
176
1.08M
    fxIntegerToString(the, profileID, buffer + length, size - length - 1);
177
1.08M
    c_strncat(buffer, ")", size - mxStringLength(buffer) - 1);
178
1.08M
  }
179
1.13M
}
180
181
void fxBufferObjectName(txMachine* the, txString buffer, txSize size, txSlot* object, txString suffix)
182
160k
{
183
160k
  txSlot* slot = mxBehaviorGetProperty(the, object, mxID(_Symbol_toStringTag), 0, XS_ANY);
184
160k
  if (slot && ((slot->kind == XS_STRING_KIND) || (slot->kind == XS_STRING_X_KIND)) && !c_isEmpty(slot->value.string)) {
185
87.3k
    fxBufferName(buffer, slot->value.string, size - mxStringLength(buffer) - 1);
186
87.3k
    c_strncat(buffer, suffix, size - mxStringLength(buffer) - 1);
187
87.3k
  }
188
160k
}
189
190
void fxBufferName(txString dst, txString src, txSize size)
191
1.23M
{
192
1.23M
  dst += mxStringLength(dst);
193
9.52M
  for (;;) {
194
9.52M
    txInteger c;
195
9.52M
    txSize length;
196
9.52M
    src = mxStringByteDecode(src, &c);
197
9.52M
    if (c == C_EOF)
198
1.23M
      break;
199
8.28M
    length = mxStringByteLength(c);
200
8.28M
    if (length > size)
201
3.25k
      break;
202
8.28M
    dst = mxStringByteEncode(dst, c);
203
8.28M
    size -= length;
204
8.28M
  }
205
1.23M
  *dst = 0;
206
1.23M
}
207
208
txString fxConcatString(txMachine* the, txSlot* a, txSlot* b)
209
9.86M
{
210
9.86M
  txSize aSize = mxStringLength(a->value.string);
211
9.86M
  txSize bSize = mxStringLength(b->value.string);
212
9.86M
  txSize resultSize = fxAddChunkSizes(the, fxAddChunkSizes(the, aSize, bSize), 1);
213
9.86M
  txString result = (txString)fxNewChunk(the, resultSize);
214
9.86M
  c_memcpy(result, a->value.string, aSize);
215
9.86M
  c_memcpy(result + aSize, b->value.string, bSize + 1);
216
9.86M
  a->value.string = result;
217
9.86M
  a->kind = XS_STRING_KIND;
218
9.86M
  return result;
219
9.86M
}
220
221
txString fxConcatStringC(txMachine* the, txSlot* a, txString b)
222
2.03M
{
223
2.03M
  txSize aSize = mxStringLength(a->value.string);
224
2.03M
  txSize bSize = mxStringLength(b);
225
2.03M
  txSize resultSize = fxAddChunkSizes(the, fxAddChunkSizes(the, aSize, bSize), 1);
226
2.03M
  txString result = C_NULL;
227
2.03M
  if (a->kind == XS_STRING_KIND)
228
2.03M
    result = (txString)fxRenewChunk(the, a->value.string, resultSize);
229
2.03M
  if (!result) {
230
75
    result = (txString)fxNewChunk(the, resultSize);
231
75
    c_memcpy(result, a->value.string, aSize);
232
75
    a->value.string = result;
233
75
    a->kind = XS_STRING_KIND;
234
75
  }
235
2.03M
  c_memcpy(result + aSize, b, bSize + 1);
236
2.03M
  return result;
237
2.03M
}
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
37.0M
{
249
37.0M
  txSize bSize = mxStringLength(b);
250
37.0M
  txSize resultSize = fxAddChunkSizes(the, bSize, 1);
251
37.0M
  txString result = (txString)fxNewChunk(the, resultSize);
252
37.0M
  c_memcpy(result, b, resultSize);
253
37.0M
  a->value.string = result;
254
37.0M
  a->kind = XS_STRING_KIND;
255
37.0M
  return result;
256
37.0M
}
257
258
txBoolean fxIsCanonicalIndex(txMachine* the, txID id)
259
382k
{
260
382k
  txSlot* key = fxGetKey(the, id);
261
382k
  if (key->flag & XS_DONT_ENUM_FLAG) {
262
360k
    txString string = key->value.key.string;
263
360k
    char buffer[256], c;
264
360k
    txNumber number;
265
360k
    c = c_read8(string);
266
360k
    if (('+' != c) && ('-' != c) && ('.' != c) && ('I' != c) && ('N' != c) && !(('0' <= c) && ('9' >= c)))
267
345k
      return 0;
268
14.4k
    number = fxStringToNumber(the, string, 1);
269
14.4k
    if (number == -0)
270
3.76k
      return 1;
271
10.7k
    fxNumberToString(the, number, buffer, sizeof(buffer), 0, 0);
272
10.7k
    if (!c_strcmp(string, buffer)) {
273
2.07k
      return 1;
274
2.07k
    }
275
10.7k
  }
276
30.9k
  return 0;
277
382k
}
278
279
int fxStringGetter(void* theStream)
280
103M
{
281
103M
  txStringStream* aStream = (txStringStream*)theStream;
282
103M
  int result = C_EOF;
283
  
284
103M
  if (aStream->offset < aStream->size) {
285
103M
    result = *(aStream->slot->value.string + aStream->offset);
286
103M
    aStream->offset++;
287
103M
  }
288
103M
  return result;
289
103M
}
290
291
int fxStringCGetter(void* theStream)
292
46.4M
{
293
46.4M
  txStringCStream* aStream = (txStringCStream*)theStream;
294
46.4M
  int result = C_EOF;
295
  
296
46.4M
  if (aStream->offset < aStream->size) {
297
46.4M
    result = *(aStream->buffer + aStream->offset);
298
46.4M
    aStream->offset++;
299
46.4M
  }
300
46.4M
  return result;
301
46.4M
}
302
303
void fxJump(txMachine* the)
304
3.11M
{
305
3.11M
  txJump* aJump = the->firstJump;
306
3.11M
  c_longjmp(aJump->buffer, 1);
307
3.11M
}