summaryrefslogtreecommitdiff
path: root/src/javascript/jsc/bindings/ZigSourceProvider.h
blob: 24ae82af880d2642e4d3698944a263fe2a12d7b7 (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
#include "headers.h"
#include "root.h"

#pragma once

namespace JSC {
class Structure;
class Identifier;
class SourceCodeKey;
class SourceProvider;
} // namespace JSC

#include "ZigConsoleClient.h"
#include <JavaScriptCore/CachedBytecode.h>
#include <JavaScriptCore/JSGlobalObject.h>
#include <JavaScriptCore/JSTypeInfo.h>
// #include <JavaScriptCore/SourceCodeKey.h>
#include <JavaScriptCore/SourceProvider.h>
#include <JavaScriptCore/Structure.h>
#include <wtf/FileSystem.h>

namespace Zig {

class SourceProvider final : public JSC::SourceProvider {
  WTF_MAKE_FAST_ALLOCATED;
  using Base = JSC::SourceProvider;
  using BytecodeCacheGenerator = JSC::BytecodeCacheGenerator;
  using UnlinkedFunctionExecutable = JSC::UnlinkedFunctionExecutable;
  using CachedBytecode = JSC::CachedBytecode;
  using UnlinkedFunctionCodeBlock = JSC::UnlinkedFunctionCodeBlock;
  using SourceCode = JSC::SourceCode;
  using CodeSpecializationKind = JSC::CodeSpecializationKind;
  using SourceOrigin = JSC::SourceOrigin;

    public:
  static Ref<SourceProvider> create(ResolvedSource resolvedSource);
  ~SourceProvider() {
    freeSourceCode();

    commitCachedBytecode();
  }

  unsigned hash() const { return m_hash; };
  StringView source() const { return StringView(m_source.get()); }
  RefPtr<JSC::CachedBytecode> cachedBytecode() {
    if (m_resolvedSource.bytecodecache_fd == 0) { return nullptr; }

    return m_cachedBytecode;
  };

  void updateCache(const UnlinkedFunctionExecutable *executable, const SourceCode &,
                   CodeSpecializationKind kind, const UnlinkedFunctionCodeBlock *codeBlock);
  void cacheBytecode(const BytecodeCacheGenerator &generator);
  void commitCachedBytecode();
  bool isBytecodeCacheEnabled() const;
  void readOrGenerateByteCodeCache(JSC::VM &vm, const JSC::SourceCode &sourceCode);
  ResolvedSource m_resolvedSource;
  int readCache(JSC::VM &vm, const JSC::SourceCode &sourceCode);
  void freeSourceCode();

    private:
  SourceProvider(ResolvedSource resolvedSource, WTF::StringImpl *sourceImpl,
                 const SourceOrigin &sourceOrigin, WTF::String &&sourceURL,
                 const TextPosition &startPosition, JSC::SourceProviderSourceType sourceType)
    : Base(sourceOrigin, WTFMove(sourceURL), startPosition, sourceType), m_source(*sourceImpl) {

    m_resolvedSource = resolvedSource;

    m_hash = resolvedSource.hash;
    getHash();
  }

  unsigned m_hash;
  unsigned getHash();
  RefPtr<JSC::CachedBytecode> m_cachedBytecode;
  Ref<WTF::StringImpl> m_source;
  bool did_free_source_code = false;
  // JSC::SourceCodeKey key;
};

} // namespace Zig