Coverage Report

Created: 2026-06-10 07:01

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