aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js/bindings/BunPlugin.h
blob: cf37b739b2b91b0e1654df653fed3daf55774143 (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
#pragma once

#include "root.h"
#include "headers-handwritten.h"
#include "JavaScriptCore/JSGlobalObject.h"
#include "JavaScriptCore/Strong.h"
#include "helpers.h"

extern "C" JSC_DECLARE_HOST_FUNCTION(jsFunctionBunPlugin);
extern "C" JSC_DECLARE_HOST_FUNCTION(jsFunctionBunPluginClear);

namespace Zig {

using namespace JSC;

class BunPlugin {
public:
    // This is a list of pairs of regexps and functions to match against
    class Group {

    public:
        // JavaScriptCore/RegularExpression does exist however it does not JIT
        // We want JIT!
        // TODO: evaluate if using JSInternalFieldImpl(2) is faster
        Vector<JSC::Strong<JSC::RegExp>> filters = {};
        Vector<JSC::Strong<JSC::JSFunction>> callbacks = {};
        BunPluginTarget target { BunPluginTargetBun };

        void append(JSC::VM& vm, JSC::RegExp* filter, JSC::JSFunction* func);
        JSFunction* find(JSC::JSGlobalObject* globalObj, String& path);
        void clear()
        {
            filters.clear();
            callbacks.clear();
        }
    };

    class Base {
    public:
        Group fileNamespace = {};
        Vector<String> namespaces = {};
        Vector<Group> groups = {};

        Group* group(const String& namespaceStr)
        {
            if (namespaceStr.isEmpty()) {
                return &fileNamespace;
            }

            for (size_t i = 0; i < namespaces.size(); i++) {
                if (namespaces[i] == namespaceStr) {
                    return &groups[i];
                }
            }

            return nullptr;
        }

        void append(JSC::VM& vm, JSC::RegExp* filter, JSC::JSFunction* func, String& namespaceString);
    };

    class OnLoad final : public Base {

    public:
        OnLoad()
            : Base()
        {
        }

        EncodedJSValue run(JSC::JSGlobalObject* globalObject, BunString* namespaceString, BunString* path);
    };

    class OnResolve final : public Base {

    public:
        OnResolve()
            : Base()
        {
        }

        EncodedJSValue run(JSC::JSGlobalObject* globalObject, BunString* namespaceString, BunString* path, BunString* importer);
    };
};

} // namespace Zig
cs: Made bun-types install as dev dependency in example (#5120)Gravatar Gordon Goldbach 1-1/+1 2023-09-12Various docs (#5201)Gravatar Colin McDonnell 4-4/+147 * Updates * Improve jest guide * Improve 2023-09-12Use git's --global flag for lockfile diffs instead of manually modifying ↵Gravatar Southpaw 1-17/+7 config files (#5143) 2023-09-12docs: fix typo in import.meta.resolve (#5146)Gravatar Jonathan Neal 1-1/+1 2023-09-12Update hot.md (#4990)Gravatar Nazeel 1-1/+1 2023-09-12Update simple.md (#4997)Gravatar Tom Redman 1-1/+1 Remove errant slash preventing the correct console log 2023-09-12fix typo and grammar errors (#5046)Gravatar xnacly 1-3/+3 2023-09-12clang and llvm on arch install v16, update to use v15 (#5069)Gravatar mi4uu 1-1/+10 2023-09-12Add missing full stop on nodejs-apis.md (#5072)Gravatar Diogo Goncalves 1-5/+5 2023-09-12udate README.md (#5127)Gravatar Toby 1-1/+1 update path 2023-09-12docs: fix typos (#5151)Gravatar Samuel Rigaud 17-18/+18 2023-09-12fix lifecycle docu (#5159)Gravatar Thomas Rupprecht 1-4/+4 2023-09-12Clean up Modules docGravatar Colin McDonnell 1-66/+73