aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js/modules/ObjectModule.cpp
blob: ebb4af32ee48e8919eb51e7911b1268e2886e84a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "ObjectModule.h"

namespace Zig {
JSC::SyntheticSourceProvider::SyntheticSourceGenerator
generateObjectModuleSourceCode(JSC::JSGlobalObject *globalObject,
                               JSC::JSObject *object) {
  JSC::VM &vm = globalObject->vm();
  gcProtectNullTolerant(object);
  return [object](JSC::JSGlobalObject *lexicalGlobalObject,
                  JSC::Identifier moduleKey,
                  Vector<JSC::Identifier, 4> &exportNames,
                  JSC::MarkedArgumentBuffer &exportValues) -> void {
    JSC::VM &vm = lexicalGlobalObject->vm();
    GlobalObject *globalObject =
        reinterpret_cast<GlobalObject *>(lexicalGlobalObject);
    JSC::EnsureStillAliveScope stillAlive(object);

    PropertyNameArray properties(vm, PropertyNameMode::Strings,
                                 PrivateSymbolMode::Exclude);
    object->getPropertyNames(globalObject, properties,
                             DontEnumPropertiesMode::Exclude);
    gcUnprotectNullTolerant(object);

    for (auto &entry : properties) {
      exportNames.append(entry);

      auto scope = DECLARE_CATCH_SCOPE(vm);
      JSValue value = object->get(globalObject, entry);
      if (scope.exception()) {
        scope.clearException();
        value = jsUndefined();
      }
      exportValues.append(value);
    }
  };
}

JSC::SyntheticSourceProvider::SyntheticSourceGenerator
generateObjectModuleSourceCodeForJSON(JSC::JSGlobalObject *globalObject,
                                      JSC::JSObject *object) {
  JSC::VM &vm = globalObject->vm();
  gcProtectNullTolerant(object);
  return [object](JSC::JSGlobalObject *lexicalGlobalObject,
                  JSC::Identifier moduleKey,
                  Vector<JSC::Identifier, 4> &exportNames,
                  JSC::MarkedArgumentBuffer &exportValues) -> void {
    JSC::VM &vm = lexicalGlobalObject->vm();
    GlobalObject *globalObject =
        reinterpret_cast<GlobalObject *>(lexicalGlobalObject);
    JSC::EnsureStillAliveScope stillAlive(object);

    PropertyNameArray properties(vm, PropertyNameMode::Strings,
                                 PrivateSymbolMode::Exclude);
    object->getPropertyNames(globalObject, properties,
                             DontEnumPropertiesMode::Exclude);
    gcUnprotectNullTolerant(object);

    for (auto &entry : properties) {
      if (entry == vm.propertyNames->defaultKeyword) {
        continue;
      }

      exportNames.append(entry);

      auto scope = DECLARE_CATCH_SCOPE(vm);
      JSValue value = object->get(globalObject, entry);
      if (scope.exception()) {
        scope.clearException();
        value = jsUndefined();
      }
      exportValues.append(value);
    }

    exportNames.append(vm.propertyNames->defaultKeyword);
    exportValues.append(object);
  };
}

JSC::SyntheticSourceProvider::SyntheticSourceGenerator
generateJSValueModuleSourceCode(JSC::JSGlobalObject *globalObject,
                                JSC::JSValue value) {

  if (value.isObject() && !JSC::isJSArray(value)) {
    return generateObjectModuleSourceCodeForJSON(globalObject,
                                                 value.getObject());
  }

  if (value.isCell())
    gcProtectNullTolerant(value.asCell());
  return [value](JSC::JSGlobalObject *lexicalGlobalObject,
                 JSC::Identifier moduleKey,
                 Vector<JSC::Identifier, 4> &exportNames,
                 JSC::MarkedArgumentBuffer &exportValues) -> void {
    JSC::VM &vm = lexicalGlobalObject->vm();
    GlobalObject *globalObject =
        reinterpret_cast<GlobalObject *>(lexicalGlobalObject);
    exportNames.append(vm.propertyNames->defaultKeyword);
    exportValues.append(value);

    if (value.isCell())
      gcUnprotectNullTolerant(value.asCell());
  };
}
} // namespace Zig