aboutsummaryrefslogtreecommitdiff
path: root/misctools/cold-jsc-start.cpp
blob: cc1a25c257ded6e633975856901dbe94cf46a097 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// print how long each step took
#define VERBOSE

//
// This loads up a JavaScriptCore global object which only has a "write"
// global function and then calls eval
//
//
// Usage:
//  ./cold-jsc-start <file>
//  ./cold-jsc-start -e "write('hey')"
//
#include "root.h"

#include <wtf/FileSystem.h>

#include <JavaScriptCore/JSGlobalObject.h>

#include <JavaScriptCore/JSArrayBufferView.h>
#include <JavaScriptCore/JSArrayBufferViewInlines.h>

#include <JavaScriptCore/Completion.h>
#include <JavaScriptCore/InitializeThreading.h>
#include <unistd.h>
#include <wtf/Stopwatch.h>

using namespace JSC;

JSC_DEFINE_HOST_FUNCTION(jsFunctionWrite, (JSC::JSGlobalObject * globalObject,
                                           JSC::CallFrame *callframe)) {

  if (callframe->argumentCount() < 1)
    return JSValue::encode(jsUndefined());

  JSValue arg1 = callframe->argument(0);
  JSValue toWriteArg = callframe->argument(1);
  auto &vm = globalObject->vm();
  auto scope = DECLARE_CATCH_SCOPE(vm);

  int32_t fd = STDOUT_FILENO;
  if (callframe->argumentCount() > 1) {
    fd = arg1.toInt32(globalObject);
    RETURN_IF_EXCEPTION(scope, encodedJSValue());
  } else {
    toWriteArg = arg1;
  }

  if (auto *buffer = jsDynamicCast<JSC::JSArrayBufferView *>(toWriteArg)) {
    auto *data = buffer->vector();
    auto length = buffer->byteLength();
    auto written = write(fd, data, length);
    return JSValue::encode(jsNumber(written));
  }

  auto string = toWriteArg.toWTFString(globalObject);
  RETURN_IF_EXCEPTION(scope, encodedJSValue());
  auto utf8 = string.utf8();
  auto length = utf8.length();
  auto written = write(fd, utf8.data(), length);
  return JSValue::encode(jsNumber(written));
}

int main(int argc, char **argv) {
  if (argc < 2) {
    fprintf(stderr, "Usage: %s <file>\n", argv[0]);
    return 1;
  }
#ifdef VERBOSE
  auto stopwatch = Stopwatch::create();
  stopwatch->start();
#endif

  {
    WTF::initializeMainThread();
    JSC::initialize();
    {
      JSC::Options::AllowUnfinalizedAccessScope scope;

      JSC::Options::useConcurrentJIT() = true;
      // JSC::Options::useSigillCrashAnalyzer() = true;
      JSC::Options::useWebAssembly() = true;
      JSC::Options::useSourceProviderCache() = true;
      // JSC::Options::useUnlinkedCodeBlockJettisoning() = false;
      JSC::Options::exposeInternalModuleLoader() = true;
      JSC::Options::useSharedArrayBuffer() = true;
      JSC::Options::useJIT() = true;
      JSC::Options::useBBQJIT() = true;
      JSC::Options::useJITCage() = false;
      JSC::Options::useShadowRealm() = true;
      JSC::Options::useResizableArrayBuffer() = true;
      JSC::Options::showPrivateScriptsInStackTraces() = true;
      JSC::Options::useSetMethods() = true;
      JSC::Options::assertOptionsAreCoherent();
    }
  }

#ifdef VERBOSE

  fprintf(stderr, "JSC::Initialize took %f ms\n",
          stopwatch->elapsedTime().milliseconds());
  stopwatch->reset();
  stopwatch->start();
#endif

  auto &vm = JSC::VM::create(JSC::HeapType::Large).leakRef();
  vm.heap.acquireAccess();

#ifdef VERBOSE
  fprintf(stderr, "JSC::VM::create took %f ms\n",
          stopwatch->elapsedTime().milliseconds());
  stopwatch->reset();
  stopwatch->start();
#endif

  JSC::JSLockHolder locker(vm);
  auto *globalObject = JSC::JSGlobalObject::create(
      vm, JSC::JSGlobalObject::createStructure(vm, JSC::jsNull()));

#ifdef VERBOSE
  fprintf(stderr, "JSC::JSGlobalObject::create took %f ms\n",
          stopwatch->elapsedTime().milliseconds());
  stopwatch->reset();
  stopwatch->start();
#endif

  JSC::gcProtect(globalObject);
  globalObject->putDirectNativeFunction(
      vm, globalObject,
      PropertyName(JSC::Identifier::fromString(vm, "write"_s)), 0,
      jsFunctionWrite, ImplementationVisibility::Public, JSC::NoIntrinsic,
      JSC::PropertyAttribute::ReadOnly | 0);

  vm.ref();
  if (argc > 2) {
    auto source =
        JSC::makeSource(WTF::String::fromUTF8(argv[argc - 1]),
                        SourceOrigin(WTF::URL("file://eval.js"_s)),
                        JSC::SourceTaintedOrigin::Untainted, "eval.js"_s);

    NakedPtr<Exception> evaluationException;
    JSValue returnValue =
        JSC::profiledEvaluate(globalObject, ProfilingReason::API, source,
                              globalObject, evaluationException);

#ifdef VERBOSE
    fprintf(stderr, "\neval took %f ms\n",
            stopwatch->elapsedTime().milliseconds());
    stopwatch->reset();

#endif

    if (evaluationException) {
      fprintf(
          stderr, "Exception: %s\n",
          evaluationException->value().toWTFString(globalObject).utf8().data());
      return 1;
    } else {
      return 0;
    }
  }

  WTF::String fileURLString = WTF::String::fromUTF8(argv[argc - 1]);

  if (auto contents = WTF::FileSystemImpl::readEntireFile(fileURLString)) {
    auto source =
        JSC::makeSource(WTF::String::fromUTF8(contents.value()),
                        SourceOrigin(WTF::URL(fileURLString)),
                        JSC::SourceTaintedOrigin::Untainted, fileURLString);

    NakedPtr<Exception> evaluationException;
    JSValue returnValue =
        JSC::profiledEvaluate(globalObject, ProfilingReason::API, source,
                              globalObject, evaluationException);

#ifdef VERBOSE
    fprintf(stderr, "eval took %f ms\n",
            stopwatch->elapsedTime().milliseconds());
    stopwatch->reset();
#endif

    if (evaluationException) {
      fprintf(
          stderr, "Exception: %s\n",
          evaluationException->value().toWTFString(globalObject).utf8().data());
      return 1;
    } else {
      return 0;
    }
  } else {
    fprintf(stderr, "Could not read file %s\n", argv[argc - 1]);
    return 1;
  }
}