File
Blob: src/workerd/jsg/resource.c++
| 1 | // Copyright (c) 2017-2022 Cloudflare, Inc. |
| 2 | // Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | // https://opensource.org/licenses/Apache-2.0 |
| 4 | |
| 5 | #include "jsg.h" |
| 6 | #include "setup.h" |
| 7 | |
| 8 | namespace workerd::jsg { |
| 9 | |
| 10 | // TODO(cleanup): Factor out toObject(), getInterned() into some sort of v8 tools module? |
| 11 | |
| 12 | void exposeGlobalScopeType(v8::Isolate* isolate, v8::Local<v8::Context> context) { |
| 13 | auto global = context->Global(); |
| 14 | |
| 15 | const auto toObject = [context]( |
| 16 | v8::Local<v8::Value> value) { return check(value->ToObject(context)); }; |
| 17 | const auto getInterned = [isolate, context](v8::Local<v8::Object> object, const char* s) { |
| 18 | return check(object->Get(context, v8StrIntern(isolate, s))); |
| 19 | }; |
| 20 | |
| 21 | auto constructor = getInterned(global, "constructor"); |
| 22 | auto name = getInterned(toObject(constructor), "name"); |
| 23 | |
| 24 | KJ_ASSERT(check(global->Set(context, name, constructor))); |
| 25 | } |
| 26 | |
| 27 | v8::Local<v8::Symbol> getSymbolDispose(v8::Isolate* isolate) { |
| 28 | return v8::Symbol::GetDispose(isolate); |
| 29 | } |
| 30 | |
| 31 | void throwIfConstructorCalledAsFunction( |
| 32 | const v8::FunctionCallbackInfo<v8::Value>& args, const std::type_info& type) { |
| 33 | if (!args.IsConstructCall()) { |
| 34 | throwTypeError(args.GetIsolate(), |
| 35 | kj::str("Failed to construct '", typeName(type), |
| 36 | "': Please use the 'new' operator, this object constructor cannot be called " |
| 37 | "as a function.")); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | void scheduleUnimplementedConstructorError( |
| 42 | const v8::FunctionCallbackInfo<v8::Value>& args, const std::type_info& type) { |
| 43 | auto isolate = args.GetIsolate(); |
| 44 | isolate->ThrowError(v8StrIntern(isolate, |
| 45 | kj::str("Failed to construct '", typeName(type), "': the constructor is not implemented."))); |
| 46 | } |
| 47 | |
| 48 | void scheduleUnimplementedMethodError(const v8::FunctionCallbackInfo<v8::Value>& args, |
| 49 | const std::type_info& type, |
| 50 | const char* methodName) { |
| 51 | auto isolate = args.GetIsolate(); |
| 52 | isolate->ThrowError(v8StrIntern(isolate, |
| 53 | kj::str("Failed to execute '", methodName, "' on '", typeName(type), |
| 54 | "': the method is not implemented."))); |
| 55 | } |
| 56 | |
| 57 | void scheduleUnimplementedPropertyError( |
| 58 | v8::Isolate* isolate, const std::type_info& type, const char* propertyName) { |
| 59 | isolate->ThrowError(v8StrIntern(isolate, |
| 60 | kj::str("Failed to get the '", propertyName, "' property on '", typeName(type), |
| 61 | "': the property is not implemented."))); |
| 62 | } |
| 63 | |
| 64 | } // namespace workerd::jsg |