Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow an isolate to have a simple random uuid #280

Merged
merged 1 commit into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/workerd/jsg/jsg-test.c++
Original file line number Diff line number Diff line change
Expand Up @@ -359,4 +359,21 @@ KJ_TEST("Test JSG_CALLABLE") {
}

} // namespace

// ========================================================================================

struct IsolateUuidContext: public Object {
JSG_RESOURCE_TYPE(IsolateUuidContext) {}
};
JSG_DECLARE_ISOLATE_TYPE(IsolateUuidIsolate, IsolateUuidContext);

KJ_TEST("jsg::Lock getUuid") {
IsolateUuidIsolate isolate(v8System);
IsolateUuidIsolate::Lock lock(isolate);
// Returns the same value
KJ_ASSERT(lock.getUuid() == lock.getUuid());
KJ_ASSERT(isolate.getUuid() == lock.getUuid());
KJ_ASSERT(lock.getUuid().size() == 36);
}

} // namespace workerd::jsg::test
4 changes: 4 additions & 0 deletions src/workerd/jsg/jsg.c++
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ void Lock::requestGcForTesting() const {
v8::Isolate::GarbageCollectionType::kFullGarbageCollection);
}

kj::StringPtr Lock::getUuid() const {
return IsolateBase::from(v8Isolate).getUuid();
}

v8::Local<v8::Private> Lock::getPrivateSymbolFor(Lock::PrivateSymbols symbol) {
return IsolateBase::from(v8Isolate).getPrivateSymbolFor(symbol);
}
Expand Down
4 changes: 4 additions & 0 deletions src/workerd/jsg/jsg.h
Original file line number Diff line number Diff line change
Expand Up @@ -2017,6 +2017,10 @@ class Lock {
// it will throw. If a need for a minor GC is needed look at the call in jsg.c++ and the
// implementation in setup.c++. Use responsibly.

kj::StringPtr getUuid() const;
// Returns a random UUID for this isolate instance. This is largely intended for logging and
// diagnostic purposes.

#define V(name, _) name,
enum PrivateSymbols {
JSG_PRIVATE_SYMBOLS(V)
Expand Down
7 changes: 7 additions & 0 deletions src/workerd/jsg/setup.c++
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "setup.h"
#include "async-context.h"
#include <workerd/util/uuid.h>
#include <cxxabi.h>
#include "libplatform/libplatform.h"
#include <ucontext.h>
Expand Down Expand Up @@ -641,4 +642,10 @@ kj::Maybe<kj::StringPtr> getJsStackTrace(void* ucontext, kj::ArrayPtr<char> scra
return kj::StringPtr(scratch.begin(), pos - scratch.begin());
}

kj::StringPtr IsolateBase::getUuid() {
// Lazily create a random UUID for this isolate.
KJ_IF_MAYBE(u, uuid) { return *u; }
return uuid.emplace(randomUUID(nullptr));
}

} // namespace workerd::jsg
4 changes: 4 additions & 0 deletions src/workerd/jsg/setup.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ class IsolateBase {

v8::Local<v8::Private> getPrivateSymbolFor(Lock::PrivateSymbols symbol);

kj::StringPtr getUuid();
// Returns a random UUID for this isolate instance.

private:
template <typename TypeWrapper>
friend class Isolate;
Expand Down Expand Up @@ -136,6 +139,7 @@ class IsolateBase {

const V8System& system;
v8::Isolate* ptr;
kj::Maybe<kj::String> uuid;
bool evalAllowed = false;
bool captureThrowsAsRejections = false;
// The Web Platform API specifications require that any API that returns a JavaScript Promise
Expand Down