Skip to content
File

Blob: src/workerd/jsg/resource-test.c++

29.1 KB
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-test.h"
6 
7#include <workerd/jsg/resource-test.capnp.h>
8 
9namespace workerd::jsg::test {
10namespace {
11 
12V8System v8System;
13class ContextGlobalObject: public Object, public ContextGlobal {};
14 
15struct BoxContext: public ContextGlobalObject {
16 JSG_RESOURCE_TYPE(BoxContext) {
17 JSG_NESTED_TYPE(NumberBox);
18 JSG_NESTED_TYPE(BoxBox);
19 }
20};
21JSG_DECLARE_ISOLATE_TYPE(BoxIsolate, BoxContext, NumberBox, BoxBox);
22 
23KJ_TEST("constructors and properties") {
24 Evaluator<BoxContext, BoxIsolate> e(v8System);
25 e.expectEval("new NumberBox(123).value", "number", "123");
26 e.expectEval("new NumberBox(123).boxed.value", "number", "123");
27 e.expectEval("new BoxBox(new NumberBox(123), 321).inner.value", "number", "444");
28 e.expectEval("var n = new NumberBox(123);\n"
29 "n.value = 321;\n"
30 "n.getValue()",
31 "number", "321");
32 e.expectEval("var n = new NumberBox(123);\n"
33 "n.boxed = new NumberBox(321);\n"
34 "n.getValue()",
35 "number", "321");
36 e.expectEval("new NumberBox(123) instanceof NumberBox", "boolean", "true");
37 e.expectEval("new NumberBox(123) instanceof BoxBox", "boolean", "false");
38}
39 
40KJ_TEST("methods") {
41 Evaluator<BoxContext, BoxIsolate> e(v8System);
42 e.expectEval("var n = new NumberBox(123);\n"
43 "n.increment();\n"
44 "n.getValue()",
45 "number", "124");
46 
47 e.expectEval("var n = new NumberBox(123);\n"
48 "n.incrementBy(321);\n"
49 "n.getValue()",
50 "number", "444");
51 
52 e.expectEval("var n = new NumberBox(123);\n"
53 "n.incrementByBox(new NumberBox(321));\n"
54 "n.getValue()",
55 "number", "444");
56 
57 e.expectEval("var n = new NumberBox(123);\n"
58 "n.add(321)",
59 "number", "444");
60 
61 e.expectEval("var n = new NumberBox(123);\n"
62 "n.addBox(new NumberBox(321))",
63 "number", "444");
64 
65 e.expectEval("var n = new NumberBox(123);\n"
66 "n.addReturnBox(321).value",
67 "number", "444");
68 
69 e.expectEval("var n = new NumberBox(123);\n"
70 "n.addMultiple(new NumberBox(321), 111, new NumberBox(2222))",
71 "number", "2777");
72 
73 e.expectEval("var n = new NumberBox(123);\n"
74 "new n.increment();",
75 "throws", "TypeError: n.increment is not a constructor");
76}
77 
78// ========================================================================================
79 
80struct Mixin {
81 int getValue() {
82 return i;
83 }
84 Mixin(int i): i(i) {}
85 int i;
86};
87struct InheritsMixin: public Object, public Mixin {
88 InheritsMixin(int i): Mixin(i) {}
89 
90 JSG_RESOURCE_TYPE(InheritsMixin) {
91 JSG_METHOD(getValue);
92 }
93};
94struct InheritsMixinContext: public ContextGlobalObject {
95 Ref<InheritsMixin> makeInheritsMixin(jsg::Lock& js, int i) {
96 return js.alloc<InheritsMixin>(i);
97 }
98 
99 JSG_RESOURCE_TYPE(InheritsMixinContext) {
100 JSG_METHOD(makeInheritsMixin);
101 }
102};
103JSG_DECLARE_ISOLATE_TYPE(InheritsMixinIsolate, InheritsMixinContext, InheritsMixin);
104 
105KJ_TEST("JSG_METHODs can be implemented by mixins") {
106 Evaluator<InheritsMixinContext, InheritsMixinIsolate> e(v8System);
107 e.expectEval("makeInheritsMixin(12345).getValue()", "number", "12345");
108}
109 
110// ========================================================================================
111 
112struct PrototypePropertyObject: public Object {
113 double value;
114 
115 PrototypePropertyObject(double value): value(value) {}
116 
117 static Ref<PrototypePropertyObject> constructor(jsg::Lock& js, double value) {
118 return js.alloc<PrototypePropertyObject>(value);
119 }
120 
121 double getValue() {
122 return value;
123 }
124 void setValue(double v) {
125 value = v;
126 }
127 
128 JSG_RESOURCE_TYPE(PrototypePropertyObject) {
129 JSG_PROTOTYPE_PROPERTY(value, getValue, setValue);
130 }
131};
132 
133struct PropContext: public ContextGlobalObject {
134 PropContext(): contextProperty(kj::str("default-context-property-value")) {}
135 
136 kj::StringPtr getContextProperty() {
137 return contextProperty;
138 }
139 void setContextProperty(kj::String s) {
140 contextProperty = kj::mv(s);
141 }
142 
143 JSG_RESOURCE_TYPE(PropContext) {
144 JSG_METHOD(getContextProperty);
145 JSG_METHOD(setContextProperty);
146 JSG_INSTANCE_PROPERTY(contextProperty, getContextProperty, setContextProperty);
147 
148 JSG_NESTED_TYPE(PrototypePropertyObject);
149 }
150 
151 private:
152 kj::String contextProperty;
153};
154JSG_DECLARE_ISOLATE_TYPE(PropIsolate, PropContext, PrototypePropertyObject);
155 
156const auto kIllegalInvocation =
157 "TypeError: Illegal invocation: function called with incorrect `this` reference. "
158 "See https://developers.cloudflare.com/workers/observability/errors/#illegal-invocation-errors for details."_kj;
159 
160KJ_TEST("context methods and properties") {
161 Evaluator<PropContext, PropIsolate> e(v8System);
162 e.expectEval("getContextProperty()", "string", "default-context-property-value");
163 e.expectEval("setContextProperty('foo');\n"
164 "getContextProperty()",
165 "string", "foo");
166 
167 e.expectEval("contextProperty", "string", "default-context-property-value");
168 e.expectEval("contextProperty = 'foo'; getContextProperty()", "string", "foo");
169 
170 e.expectEval("this.getContextProperty()", "string", "default-context-property-value");
171 e.expectEval("this.setContextProperty('foo');\n"
172 "getContextProperty()",
173 "string", "foo");
174 
175 e.expectEval("this.contextProperty", "string", "default-context-property-value");
176 e.expectEval("this.contextProperty = 'foo'; getContextProperty()", "string", "foo");
177 
178 e.expectEval("let p = new PrototypePropertyObject(123);\n"
179 "let o = {};\n"
180 "o.__proto__ = p.__proto__;\n"
181 "o.value",
182 "throws", kIllegalInvocation);
183 e.expectEval("let p = new PrototypePropertyObject(123);\n"
184 "let o = {};\n"
185 "o.__proto__ = p.__proto__;\n"
186 "o.value = 123",
187 "throws", kIllegalInvocation);
188 
189 e.expectEval("class P2 extends PrototypePropertyObject {\n"
190 " constructor(v) { super(v); }\n"
191 "}\n"
192 "let p = new P2(123);\n"
193 "p.value",
194 "number", "123");
195}
196 
197// ========================================================================================
198 
199struct NonConstructibleContext: public ContextGlobalObject {
200 struct NonConstructible: public Object {
201 NonConstructible(double x): x(x) {}
202 
203 double x;
204 
205 double method() {
206 return x;
207 }
208 
209 JSG_RESOURCE_TYPE(NonConstructible) {
210 JSG_METHOD(method);
211 }
212 };
213 
214 Ref<NonConstructible> getNonConstructible(jsg::Lock& js, double x) {
215 return js.alloc<NonConstructible>(x);
216 }
217 
218 JSG_RESOURCE_TYPE(NonConstructibleContext) {
219 JSG_NESTED_TYPE(NonConstructible);
220 JSG_METHOD(getNonConstructible);
221 }
222};
223JSG_DECLARE_ISOLATE_TYPE(
224 NonConstructibleIsolate, NonConstructibleContext, NonConstructibleContext::NonConstructible);
225 
226KJ_TEST("non-constructible types can't be constructed") {
227 Evaluator<NonConstructibleContext, NonConstructibleIsolate> e(v8System);
228 e.expectEval("new NonConstructible().method()", "throws", "TypeError: Illegal constructor");
229 
230 e.expectEval("getNonConstructible(12321).method()", "number", "12321");
231 
232 e.expectEval("getNonConstructible(12321) instanceof NonConstructible", "boolean", "true");
233}
234 
235// ========================================================================================
236 
237struct IterableContext: public ContextGlobalObject {
238 class Iterable: public Object {
239 public:
240 static Ref<Iterable> constructor(jsg::Lock& js) {
241 return js.alloc<Iterable>();
242 }
243 
244 class Iterator: public Object {
245 public:
246 struct NextValue {
247 bool done;
248 Optional<int> value;
249 JSG_STRUCT(done, value);
250 };
251 
252 explicit Iterator(Ref<Iterable> parentParam)
253 : parent(kj::mv(parentParam)),
254 cursor(parent->values) {}
255 
256 NextValue next() {
257 if (cursor == parent->values + sizeof(parent->values) / sizeof(*parent->values)) {
258 return {.done = true, .value = kj::none};
259 }
260 return {.done = false, .value = *cursor++};
261 }
262 
263 v8::Local<v8::Object> self(const v8::FunctionCallbackInfo<v8::Value>& info) {
264 // Helper to make this iterator itself iterable. This allows code like
265 // `for (let k of iterable.entries())` to work.
266 return info.This();
267 }
268 
269 JSG_RESOURCE_TYPE(Iterator) {
270 JSG_INHERIT_INTRINSIC(v8::kIteratorPrototype);
271 JSG_METHOD(next);
272 JSG_ITERABLE(self);
273 }
274 
275 private:
276 Ref<Iterable> parent;
277 int* cursor;
278 };
279 
280 Ref<Iterator> entries(const v8::FunctionCallbackInfo<v8::Value>& info) {
281 auto& js = jsg::Lock::from(info.GetIsolate());
282 return js.alloc<Iterator>(JSG_THIS);
283 }
284 
285 JSG_RESOURCE_TYPE(Iterable) {
286 JSG_METHOD(entries);
287 JSG_ITERABLE(entries);
288 }
289 
290 private:
291 int values[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
292 // In real code, this data structure could be more complex, and we would need to think about
293 // iterator invalidation, which might require storing back-references from the parent iterable
294 // to all of its live iterators to make sure they can be nulled out if necessary. But then we
295 // need to worry about circular references ...
296 };
297 
298 JSG_RESOURCE_TYPE(IterableContext) {
299 JSG_NESTED_TYPE(Iterable);
300 }
301};
302JSG_DECLARE_ISOLATE_TYPE(IterableIsolate,
303 IterableContext,
304 IterableContext::Iterable,
305 IterableContext::Iterable::Iterator,
306 IterableContext::Iterable::Iterator::NextValue);
307 
308KJ_TEST("Iterables can be iterated") {
309 Evaluator<IterableContext, IterableIsolate> e(v8System);
310 e.expectEval("let results = [];"
311 "for (let n of new Iterable()) { results.push(n); };"
312 "'' + results.join('')",
313 "string", "0123456789");
314 e.expectEval("let results = [];"
315 "for (let n of new Iterable().entries()) { results.push(n); };"
316 "'' + results.join('')",
317 "string", "0123456789");
318 e.expectEval(
319 "let arrayIterator = [][Symbol.iterator]();"
320 "let arrayIteratorPrototype = Object.getPrototypeOf(Object.getPrototypeOf(arrayIterator));"
321 "let iterator = new Iterable().entries();"
322 "let iteratorPrototype = Object.getPrototypeOf(Object.getPrototypeOf(iterator));"
323 "iteratorPrototype === arrayIteratorPrototype",
324 "boolean", "true");
325}
326 
327// ========================================================================================
328 
329struct StaticContext: public ContextGlobalObject {
330 struct StaticConstants: public Object {
331 static Ref<StaticConstants> constructor(jsg::Lock& js) {
332 return js.alloc<StaticConstants>();
333 }
334 
335 static constexpr double DOUBLE = 1.5;
336 static constexpr int INT = 123;
337 static constexpr bool BOOL = true;
338 static constexpr kj::StringPtr STRING = "a static constant string"_kj;
339 
340 JSG_RESOURCE_TYPE(StaticConstants) {
341 JSG_STATIC_CONSTANT(DOUBLE);
342 JSG_STATIC_CONSTANT(INT);
343 JSG_STATIC_CONSTANT(BOOL);
344 JSG_STATIC_CONSTANT(STRING);
345 }
346 };
347 
348 struct StaticMethods: public Object {
349 static Ref<StaticMethods> constructor(jsg::Lock& js) {
350 return js.alloc<StaticMethods>();
351 }
352 
353 static v8::Local<v8::Value> passThrough(v8::Local<v8::Value> arg) {
354 return arg;
355 }
356 static v8::Local<v8::Value> passThroughWithInfo(
357 const v8::FunctionCallbackInfo<v8::Value>& info, v8::Local<v8::Value> arg) {
358 return arg;
359 }
360 
361 static void voidCall() {}
362 static void voidCallWithInfo(const v8::FunctionCallbackInfo<v8::Value>& info) {}
363 
364 static Unimplemented unimplementedStaticMethod() {
365 return {};
366 }
367 
368 static void delete_() {}
369 
370 JSG_RESOURCE_TYPE(StaticMethods) {
371 JSG_STATIC_METHOD(passThrough);
372 JSG_STATIC_METHOD(passThroughWithInfo);
373 JSG_STATIC_METHOD(voidCall);
374 JSG_STATIC_METHOD(voidCallWithInfo);
375 JSG_STATIC_METHOD_NAMED(delete, delete_);
376 JSG_STATIC_METHOD(unimplementedStaticMethod);
377 }
378 };
379 
380 struct StaticProperties: public Object {
381 static Ref<StaticProperties> constructor(jsg::Lock& js) {
382 return js.alloc<StaticProperties>();
383 }
384 
385 // Static property returning a simple value
386 static int simpleValue() {
387 return 42;
388 }
389 
390 // Static property returning a string
391 static kj::String stringValue() {
392 return kj::str("static property string");
393 }
394 
395 // Static property returning an array
396 static kj::Array<int> arrayValue() {
397 auto array = kj::heapArray<int>(3);
398 array[0] = 1;
399 array[1] = 2;
400 array[2] = 3;
401 return kj::mv(array);
402 }
403 
404 // Static property returning void (edge case)
405 static void voidProperty() {
406 // This should be handled gracefully even though it's unusual for a property
407 }
408 
409 // A getter function to be used with JSG_STATIC_READONLY_PROPERTY_NAMED
410 static double getComputedValue() {
411 return 3.14159;
412 }
413 
414 // Another named property getter
415 static kj::String getDescription() {
416 return kj::str("This is a static property");
417 }
418 
419 // Static property that takes Lock& as first parameter
420 static int withLockValue(jsg::Lock& js) {
421 // We can use the lock here if needed, but for testing just return a value
422 return 99;
423 }
424 
425 // Static property with Lock& that returns an allocated object
426 static kj::String withLockString(jsg::Lock& js) {
427 return kj::str("property with lock");
428 }
429 
430 JSG_RESOURCE_TYPE(StaticProperties) {
431 JSG_STATIC_READONLY_PROPERTY(simpleValue);
432 JSG_STATIC_READONLY_PROPERTY(stringValue);
433 JSG_STATIC_READONLY_PROPERTY(arrayValue);
434 JSG_STATIC_READONLY_PROPERTY(voidProperty);
435 JSG_STATIC_READONLY_PROPERTY_NAMED(computedValue, getComputedValue);
436 JSG_STATIC_READONLY_PROPERTY_NAMED(description, getDescription);
437 JSG_STATIC_READONLY_PROPERTY(withLockValue);
438 JSG_STATIC_READONLY_PROPERTY(withLockString);
439 }
440 };
441 
442 JSG_RESOURCE_TYPE(StaticContext) {
443 JSG_NESTED_TYPE(StaticConstants);
444 JSG_NESTED_TYPE(StaticMethods);
445 JSG_NESTED_TYPE(StaticProperties);
446 }
447};
448JSG_DECLARE_ISOLATE_TYPE(StaticIsolate,
449 StaticContext,
450 StaticContext::StaticConstants,
451 StaticContext::StaticMethods,
452 StaticContext::StaticProperties);
453 
454KJ_TEST("Static constants are exposed as constructor properties") {
455 Evaluator<StaticContext, StaticIsolate> e(v8System);
456 e.expectEval("StaticConstants.DOUBLE === 1.5", "boolean", "true");
457 e.expectEval("StaticConstants.INT === 123", "boolean", "true");
458 e.expectEval("StaticConstants.BOOL === true", "boolean", "true");
459 e.expectEval("StaticConstants.STRING === 'a static constant string'", "boolean", "true");
460}
461KJ_TEST("Static constants are exposed as constructor prototype properties") {
462 Evaluator<StaticContext, StaticIsolate> e(v8System);
463 e.expectEval("StaticConstants.prototype.DOUBLE === 1.5", "boolean", "true");
464 e.expectEval("StaticConstants.prototype.INT === 123", "boolean", "true");
465 e.expectEval("StaticConstants.prototype.BOOL === true", "boolean", "true");
466 e.expectEval(
467 "StaticConstants.prototype.STRING === 'a static constant string'", "boolean", "true");
468}
469KJ_TEST("Static constants are exposed as object instance properties") {
470 Evaluator<StaticContext, StaticIsolate> e(v8System);
471 e.expectEval("new StaticConstants().DOUBLE === 1.5", "boolean", "true");
472 e.expectEval("new StaticConstants().INT === 123", "boolean", "true");
473 e.expectEval("new StaticConstants().BOOL === true", "boolean", "true");
474 e.expectEval("new StaticConstants().STRING === 'a static constant string'", "boolean", "true");
475}
476KJ_TEST("Static constants are exposed as object instance prototype properties") {
477 Evaluator<StaticContext, StaticIsolate> e(v8System);
478 e.expectEval("Object.getPrototypeOf(new StaticConstants()).DOUBLE === 1.5", "boolean", "true");
479 e.expectEval("Object.getPrototypeOf(new StaticConstants()).INT === 123", "boolean", "true");
480 e.expectEval("Object.getPrototypeOf(new StaticConstants()).BOOL === true", "boolean", "true");
481 e.expectEval("Object.getPrototypeOf(new StaticConstants()).STRING === 'a static constant string'",
482 "boolean", "true");
483}
484KJ_TEST("Static methods are exposed as constructor properties") {
485 Evaluator<StaticContext, StaticIsolate> e(v8System);
486 e.expectEval("StaticMethods.passThrough(true)", "boolean", "true");
487 e.expectEval("StaticMethods.passThroughWithInfo(true)", "boolean", "true");
488 e.expectEval("StaticMethods.voidCall(); true;", "boolean", "true");
489 e.expectEval("StaticMethods.voidCallWithInfo(); true;", "boolean", "true");
490 e.expectEval("StaticMethods.delete(); true;", "boolean", "true");
491 e.expectEval("StaticMethods.unimplementedStaticMethod()", "throws",
492 "Error: Failed to execute 'unimplementedStaticMethod' on 'StaticMethods': "
493 "the method is not implemented.");
494 e.expectEval("new StaticMethods.passThrough(true);", "throws",
495 "TypeError: StaticMethods.passThrough is not a constructor");
496}
497KJ_TEST("Static methods are not exposed as constructor prototype properties") {
498 Evaluator<StaticContext, StaticIsolate> e(v8System);
499 e.expectEval("typeof StaticMethods.prototype.passThrough === 'undefined'\n"
500 "&& typeof StaticMethods.prototype.passThroughWithInfo === 'undefined'\n"
501 "&& typeof StaticMethods.prototype.voidCall === 'undefined'\n"
502 "&& typeof StaticMethods.prototype.voidCallWithInfo === 'undefined'\n"
503 "&& typeof StaticMethods.prototype.delete === 'undefined'\n"
504 "&& typeof StaticMethods.prototype.unimplementedStaticMethod === 'undefined'",
505 "boolean", "true");
506}
507KJ_TEST("Static methods are not exposed as object instance properties") {
508 Evaluator<StaticContext, StaticIsolate> e(v8System);
509 e.expectEval("let obj = new StaticMethods();\n"
510 "typeof obj.passThrough === 'undefined'\n"
511 "&& typeof obj.passThroughWithInfo === 'undefined'\n"
512 "&& typeof obj.voidCall === 'undefined'\n"
513 "&& typeof obj.voidCallWithInfo === 'undefined'\n"
514 "&& typeof obj.delete === 'undefined'\n"
515 "&& typeof obj.unimplementedStaticMethod === 'undefined'",
516 "boolean", "true");
517}
518 
519KJ_TEST("Static properties are exposed as constructor properties") {
520 Evaluator<StaticContext, StaticIsolate> e(v8System);
521 // Test simple value property
522 e.expectEval("StaticProperties.simpleValue", "number", "42");
523 e.expectEval("StaticProperties.simpleValue === 42", "boolean", "true");
524 
525 // Test string property
526 e.expectEval("StaticProperties.stringValue", "string", "static property string");
527 e.expectEval("StaticProperties.stringValue === 'static property string'", "boolean", "true");
528 
529 // Test array property
530 e.expectEval("Array.isArray(StaticProperties.arrayValue)", "boolean", "true");
531 e.expectEval("StaticProperties.arrayValue.length", "number", "3");
532 e.expectEval("StaticProperties.arrayValue[0]", "number", "1");
533 e.expectEval("StaticProperties.arrayValue[1]", "number", "2");
534 e.expectEval("StaticProperties.arrayValue[2]", "number", "3");
535 
536 // Test void property (should be undefined)
537 e.expectEval("StaticProperties.voidProperty", "undefined", "undefined");
538 
539 // Test named properties
540 e.expectEval("StaticProperties.computedValue", "number", "3.14159");
541 e.expectEval("StaticProperties.description", "string", "This is a static property");
542 
543 // Test properties that take Lock& as first parameter
544 e.expectEval("StaticProperties.withLockValue", "number", "99");
545 e.expectEval("StaticProperties.withLockString", "string", "property with lock");
546}
547 
548KJ_TEST("Static properties are read-only") {
549 Evaluator<StaticContext, StaticIsolate> e(v8System);
550 // Attempting to write to static properties should have no effect
551 e.expectEval("StaticProperties.simpleValue = 100; StaticProperties.simpleValue", "number", "42");
552 e.expectEval("StaticProperties.stringValue = 'new'; StaticProperties.stringValue", "string",
553 "static property string");
554 e.expectEval(
555 "StaticProperties.computedValue = 0; StaticProperties.computedValue", "number", "3.14159");
556 e.expectEval(
557 "StaticProperties.withLockValue = 200; StaticProperties.withLockValue", "number", "99");
558}
559 
560KJ_TEST("Static properties are not functions") {
561 Evaluator<StaticContext, StaticIsolate> e(v8System);
562 // Static properties should not be callable as functions
563 e.expectEval("typeof StaticProperties.simpleValue", "string", "number");
564 e.expectEval("StaticProperties.simpleValue()", "throws",
565 "TypeError: StaticProperties.simpleValue is not a function");
566 e.expectEval("new StaticProperties.simpleValue()", "throws",
567 "TypeError: StaticProperties.simpleValue is not a constructor");
568}
569 
570KJ_TEST("Static properties are not exposed as instance properties") {
571 Evaluator<StaticContext, StaticIsolate> e(v8System);
572 e.expectEval("let obj = new StaticProperties();\n"
573 "typeof obj.simpleValue === 'undefined'\n"
574 "&& typeof obj.stringValue === 'undefined'\n"
575 "&& typeof obj.arrayValue === 'undefined'\n"
576 "&& typeof obj.voidProperty === 'undefined'\n"
577 "&& typeof obj.computedValue === 'undefined'\n"
578 "&& typeof obj.description === 'undefined'\n"
579 "&& typeof obj.withLockValue === 'undefined'\n"
580 "&& typeof obj.withLockString === 'undefined'",
581 "boolean", "true");
582}
583 
584KJ_TEST("Static properties are not exposed on prototype") {
585 Evaluator<StaticContext, StaticIsolate> e(v8System);
586 e.expectEval("typeof StaticProperties.prototype.simpleValue === 'undefined'\n"
587 "&& typeof StaticProperties.prototype.stringValue === 'undefined'\n"
588 "&& typeof StaticProperties.prototype.arrayValue === 'undefined'\n"
589 "&& typeof StaticProperties.prototype.voidProperty === 'undefined'\n"
590 "&& typeof StaticProperties.prototype.computedValue === 'undefined'\n"
591 "&& typeof StaticProperties.prototype.description === 'undefined'\n"
592 "&& typeof StaticProperties.prototype.withLockValue === 'undefined'\n"
593 "&& typeof StaticProperties.prototype.withLockString === 'undefined'",
594 "boolean", "true");
595}
596KJ_TEST("Static methods are not exposed as object instance prototype properties") {
597 Evaluator<StaticContext, StaticIsolate> e(v8System);
598 e.expectEval("let objProto = Object.getPrototypeOf(new StaticMethods());\n"
599 "typeof objProto.passThrough === 'undefined'\n"
600 "&& typeof objProto.passThroughWithInfo === 'undefined'\n"
601 "&& typeof objProto.voidCall === 'undefined'\n"
602 "&& typeof objProto.voidCallWithInfo === 'undefined'\n"
603 "&& typeof objProto.delete === 'undefined'\n"
604 "&& typeof objProto.unimplementedStaticMethod === 'undefined'",
605 "boolean", "true");
606}
607KJ_TEST("Static methods can be monkey-patched") {
608 Evaluator<StaticContext, StaticIsolate> e(v8System);
609 e.expectEval("StaticMethods.passThrough = function(a) { return false; };\n"
610 "StaticMethods.passThrough(true)",
611 "boolean", "false");
612}
613 
614// ========================================================================================
615 
616struct ReflectionContext: public ContextGlobalObject {
617 struct Super: public Object {
618 JSG_RESOURCE_TYPE(Super) {}
619 };
620 
621 struct Reflector: public Super {
622 static jsg::Ref<Reflector> constructor(jsg::Lock& js) {
623 auto result = js.alloc<Reflector>();
624 
625 // Check reflection returns null when wrapper isn't allocated.
626 KJ_EXPECT(result->intReflector.get(js.v8Isolate, "foo") == kj::none);
627 KJ_EXPECT(result->stringReflector.get(js.v8Isolate, "foo") == kj::none);
628 
629 return result;
630 }
631 
632 kj::Maybe<int> getIntReflection(jsg::Lock& js, kj::String name) {
633 return intReflector.get(js.v8Isolate, name);
634 }
635 kj::Maybe<kj::String> getStringReflection(jsg::Lock& js, kj::String name) {
636 return stringReflector.get(js.v8Isolate, name);
637 }
638 
639 PropertyReflection<int> intReflector;
640 PropertyReflection<kj::String> stringReflector;
641 
642 JSG_RESOURCE_TYPE(Reflector) {
643 JSG_INHERIT(Super);
644 JSG_METHOD(getIntReflection);
645 JSG_METHOD(getStringReflection);
646 }
647 JSG_REFLECTION(intReflector, stringReflector);
648 };
649 
650 jsg::Ref<Reflector> makeReflector(jsg::Lock& js) {
651 return js.alloc<Reflector>();
652 }
653 
654 jsg::Ref<Super> makeSuper(jsg::Lock& js) {
655 return js.alloc<Reflector>();
656 }
657 
658 JSG_RESOURCE_TYPE(ReflectionContext) {
659 JSG_NESTED_TYPE(Reflector);
660 JSG_METHOD(makeReflector);
661 JSG_METHOD(makeSuper);
662 }
663};
664JSG_DECLARE_ISOLATE_TYPE(
665 ReflectionIsolate, ReflectionContext, ReflectionContext::Super, ReflectionContext::Reflector);
666 
667KJ_TEST("PropertyReflection works") {
668 Evaluator<ReflectionContext, ReflectionIsolate> e(v8System);
669 e.expectEval("let r = new Reflector; r.getIntReflection('foo')", "object", "null");
670 e.expectEval("let r = new Reflector; r.foo = 123; r.getIntReflection('foo')", "number", "123");
671 e.expectEval("let r = new Reflector; r.foo = 123; r.getStringReflection('foo')", "string", "123");
672 
673 e.expectEval("let r = makeReflector(); r.foo = 123; r.getIntReflection('foo')", "number", "123");
674 e.expectEval("let r = makeSuper(); r.foo = 123; r.getIntReflection('foo')", "number", "123");
675}
676 
677// ========================================================================================
678 
679struct InjectLockContext: public ContextGlobalObject {
680 struct Thingy: public Object {
681 Thingy(int val, v8::Isolate* v8Isolate): val(val), v8Isolate(v8Isolate) {}
682 int val;
683 v8::Isolate* v8Isolate;
684 
685 static Ref<Thingy> constructor(Lock& js, int val) {
686 return js.alloc<Thingy>(val, js.v8Isolate);
687 }
688 
689 int frob(Lock& js, int val2) {
690 KJ_ASSERT(js.v8Isolate == v8Isolate);
691 return val + val2;
692 }
693 
694 int getVal(Lock& js) {
695 KJ_ASSERT(js.v8Isolate == v8Isolate);
696 return val;
697 }
698 
699 void setVal(Lock& js, int val) {
700 KJ_ASSERT(js.v8Isolate == v8Isolate);
701 this->val = val;
702 }
703 
704 static int borf(Lock& js, int val) {
705 return val * 2;
706 }
707 
708 JSG_RESOURCE_TYPE(Thingy) {
709 JSG_METHOD(frob);
710 JSG_PROTOTYPE_PROPERTY(val, getVal, setVal);
711 JSG_STATIC_METHOD(borf);
712 }
713 };
714 
715 JSG_RESOURCE_TYPE(InjectLockContext) {
716 JSG_NESTED_TYPE(Thingy);
717 }
718};
719JSG_DECLARE_ISOLATE_TYPE(InjectLockIsolate, InjectLockContext, InjectLockContext::Thingy);
720 
721KJ_TEST("Methods can take Lock& as first parameter") {
722 Evaluator<InjectLockContext, InjectLockIsolate> e(v8System);
723 e.expectEval("let t = new Thingy(123); t.val", "number", "123");
724}
725 
726// ========================================================================================
727 
728struct JsBundleContext: public ContextGlobalObject {
729 JSG_RESOURCE_TYPE(JsBundleContext) {
730 JSG_CONTEXT_JS_BUNDLE(BUILTIN_BUNDLE);
731 }
732};
733JSG_DECLARE_ISOLATE_TYPE(JsBundleIsolate, JsBundleContext);
734 
735KJ_TEST("expectEvalModule function works") {
736 Evaluator<JsBundleContext, JsBundleIsolate, decltype(nullptr), JsBundleIsolate_TypeWrapper> e(
737 v8System);
738 e.expectEvalModule("export function run() { return 123; }", "number", "123");
739}
740 
741KJ_TEST("bundle installed works") {
742 Evaluator<JsBundleContext, JsBundleIsolate, decltype(nullptr), JsBundleIsolate_TypeWrapper> e(
743 v8System);
744 e.expectEvalModule(R"(
745 import * as b from "test:resource-test-builtin";
746 export function run() { return b.builtinFunction(); }
747 )",
748 "string", "THIS_IS_BUILTIN_FUNCTION");
749}
750 
751// ========================================================================================
752// Regression test for https://github.com/cloudflare/workerd/issues/4147
753 
754struct InspectableObject: public Object {
755 double value;
756 
757 explicit InspectableObject(double value): value(value) {}
758 
759 static Ref<InspectableObject> constructor(jsg::Lock& js, double value) {
760 return js.alloc<InspectableObject>(value);
761 }
762 
763 double getValue() {
764 return value;
765 }
766 
767 kj::String inspectState() {
768 return kj::str("active");
769 }
770 
771 JSG_RESOURCE_TYPE(InspectableObject) {
772 JSG_METHOD(getValue);
773 JSG_READONLY_PROTOTYPE_PROPERTY(value, getValue);
774 JSG_INSPECT_PROPERTY(state, inspectState);
775 }
776};
777 
778struct InspectContext: public ContextGlobalObject {
779 JSG_RESOURCE_TYPE(InspectContext) {
780 JSG_NESTED_TYPE(InspectableObject);
781 }
782};
783JSG_DECLARE_ISOLATE_TYPE(InspectIsolate, InspectContext, InspectableObject);
784 
785KJ_TEST("Object.getOwnPropertyDescriptors does not throw on prototype with inspect properties") {
786 Evaluator<InspectContext, InspectIsolate> e(v8System);
787 
788 // The core regression: getOwnPropertyDescriptors must not throw.
789 e.expectEval("JSON.stringify(typeof Object.getOwnPropertyDescriptors("
790 " InspectableObject.prototype))",
791 "string", "\"object\"");
792 
793 // Methods should appear as data descriptors.
794 e.expectEval("typeof Object.getOwnPropertyDescriptors("
795 " InspectableObject.prototype).getValue.value",
796 "string", "function");
797 
798 // The prototype property 'value' should appear as an accessor descriptor.
799 e.expectEval("typeof Object.getOwnPropertyDescriptors("
800 " InspectableObject.prototype).value.get",
801 "string", "function");
802 
803 // Instances should also tolerate getOwnPropertyDescriptors.
804 e.expectEval("JSON.stringify(typeof Object.getOwnPropertyDescriptors("
805 " new InspectableObject(42)))",
806 "string", "\"object\"");
807}
808 
809// ========================================================================================
810 
811} // namespace
812} // namespace workerd::jsg::test