Coverage Report

Created: 2025-12-14 06:18

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