@@ -33,8 +33,6 @@ const JSErrorFormatString *FastlyGetErrorMessage(void *userRef, unsigned errorNu
33
33
34
34
namespace {
35
35
36
- api::Engine *ENGINE;
37
-
38
36
bool enableDebugLogging (JSContext *cx, unsigned argc, JS::Value *vp) {
39
37
JS::CallArgs args = CallArgsFromVp (argc, vp);
40
38
if (!args.requireAtLeast (cx, __func__, 1 ))
@@ -51,6 +49,13 @@ JS::PersistentRooted<JSObject *> Fastly::baseURL;
51
49
JS::PersistentRooted<JSString *> Fastly::defaultBackend;
52
50
bool Fastly::allowDynamicBackends = false ;
53
51
52
+ bool Fastly::version_get (JSContext *cx, unsigned argc, JS::Value *vp) {
53
+ JS::CallArgs args = CallArgsFromVp (argc, vp);
54
+ JS::RootedString version_str (cx, JS_NewStringCopyN (cx, RUNTIME_VERSION, strlen (RUNTIME_VERSION)));
55
+ args.rval ().setString (version_str);
56
+ return true ;
57
+ }
58
+
54
59
bool Env::env_get (JSContext *cx, unsigned argc, JS::Value *vp) {
55
60
JS::CallArgs args = CallArgsFromVp (argc, vp);
56
61
if (!args.requireAtLeast (cx, " fastly.env.get" , 1 ))
@@ -306,145 +311,151 @@ const JSPropertySpec Fastly::properties[] = {
306
311
JS_PSGS (" defaultBackend" , defaultBackend_get, defaultBackend_set, JSPROP_ENUMERATE),
307
312
JS_PSGS (" allowDynamicBackends" , allowDynamicBackends_get, allowDynamicBackends_set,
308
313
JSPROP_ENUMERATE),
314
+ JS_PSG (" sdkVersion" , version_get, JSPROP_ENUMERATE),
309
315
JS_PS_END};
310
316
311
317
bool install (api::Engine *engine) {
312
- ENGINE = engine;
313
- JS::RootedObject fastly (ENGINE->cx (), JS_NewPlainObject (ENGINE->cx ()));
318
+ JS::RootedObject fastly (engine->cx (), JS_NewPlainObject (engine->cx ()));
314
319
if (!fastly) {
315
320
return false ;
316
321
}
317
322
318
- Fastly::env.init (ENGINE ->cx (), Env::create (ENGINE ->cx ()));
323
+ Fastly::env.init (engine ->cx (), Env::create (engine ->cx ()));
319
324
if (!Fastly::env) {
320
325
return false ;
321
326
}
322
327
323
- Fastly::baseURL.init (ENGINE ->cx ());
324
- Fastly::defaultBackend.init (ENGINE ->cx ());
328
+ Fastly::baseURL.init (engine ->cx ());
329
+ Fastly::defaultBackend.init (engine ->cx ());
325
330
326
- if (!JS_DefineProperty (ENGINE ->cx (), ENGINE ->global (), " fastly" , fastly, 0 )) {
331
+ if (!JS_DefineProperty (engine ->cx (), engine ->global (), " fastly" , fastly, 0 )) {
327
332
return false ;
328
333
}
329
334
330
335
// fastly:env
331
- RootedValue env_get (ENGINE ->cx ());
332
- if (!JS_GetProperty (ENGINE ->cx (), Fastly::env, " get" , &env_get)) {
336
+ RootedValue env_get (engine ->cx ());
337
+ if (!JS_GetProperty (engine ->cx (), Fastly::env, " get" , &env_get)) {
333
338
return false ;
334
339
}
335
- RootedObject env_builtin (ENGINE ->cx (), JS_NewObject (ENGINE ->cx (), nullptr ));
336
- if (!JS_SetProperty (ENGINE ->cx (), env_builtin, " env" , env_get)) {
340
+ RootedObject env_builtin (engine ->cx (), JS_NewObject (engine ->cx (), nullptr ));
341
+ if (!JS_SetProperty (engine ->cx (), env_builtin, " env" , env_get)) {
337
342
return false ;
338
343
}
339
- RootedValue env_builtin_val (ENGINE ->cx (), JS::ObjectValue (*env_builtin));
340
- if (!ENGINE ->define_builtin_module (" fastly:env" , env_builtin_val)) {
344
+ RootedValue env_builtin_val (engine ->cx (), JS::ObjectValue (*env_builtin));
345
+ if (!engine ->define_builtin_module (" fastly:env" , env_builtin_val)) {
341
346
return false ;
342
347
}
343
348
344
349
// fastly:experimental
345
- RootedObject experimental (ENGINE ->cx (), JS_NewObject (ENGINE ->cx (), nullptr ));
346
- RootedValue experimental_val (ENGINE ->cx (), JS::ObjectValue (*experimental));
350
+ RootedObject experimental (engine ->cx (), JS_NewObject (engine ->cx (), nullptr ));
351
+ RootedValue experimental_val (engine ->cx (), JS::ObjectValue (*experimental));
347
352
// TODO(GB): implement includeBytes
348
- if (!JS_SetProperty (ENGINE ->cx (), experimental, " includeBytes" , experimental_val)) {
353
+ if (!JS_SetProperty (engine ->cx (), experimental, " includeBytes" , experimental_val)) {
349
354
return false ;
350
355
}
351
356
auto set_default_backend =
352
- JS_NewFunction (ENGINE ->cx (), &Fastly::defaultBackend_set, 1 , 0 , " setDefaultBackend" );
353
- RootedObject set_default_backend_obj (ENGINE ->cx (), JS_GetFunctionObject (set_default_backend));
354
- RootedValue set_default_backend_val (ENGINE ->cx (), ObjectValue (*set_default_backend_obj));
355
- if (!JS_SetProperty (ENGINE ->cx (), experimental, " setDefaultBackend" , set_default_backend_val)) {
357
+ JS_NewFunction (engine ->cx (), &Fastly::defaultBackend_set, 1 , 0 , " setDefaultBackend" );
358
+ RootedObject set_default_backend_obj (engine ->cx (), JS_GetFunctionObject (set_default_backend));
359
+ RootedValue set_default_backend_val (engine ->cx (), ObjectValue (*set_default_backend_obj));
360
+ if (!JS_SetProperty (engine ->cx (), experimental, " setDefaultBackend" , set_default_backend_val)) {
356
361
return false ;
357
362
}
358
363
auto allow_dynamic_backends =
359
- JS_NewFunction (ENGINE ->cx (), &Fastly::allowDynamicBackends_set, 1 , 0 , " allowDynamicBackends" );
360
- RootedObject allow_dynamic_backends_obj (ENGINE ->cx (),
364
+ JS_NewFunction (engine ->cx (), &Fastly::allowDynamicBackends_set, 1 , 0 , " allowDynamicBackends" );
365
+ RootedObject allow_dynamic_backends_obj (engine ->cx (),
361
366
JS_GetFunctionObject (allow_dynamic_backends));
362
- RootedValue allow_dynamic_backends_val (ENGINE ->cx (), ObjectValue (*allow_dynamic_backends_obj));
363
- if (!JS_SetProperty (ENGINE ->cx (), experimental, " allowDynamicBackends" ,
367
+ RootedValue allow_dynamic_backends_val (engine ->cx (), ObjectValue (*allow_dynamic_backends_obj));
368
+ if (!JS_SetProperty (engine ->cx (), experimental, " allowDynamicBackends" ,
364
369
allow_dynamic_backends_val)) {
365
370
return false ;
366
371
}
367
- if (!ENGINE->define_builtin_module (" fastly:experimental" , experimental_val)) {
372
+ RootedString version_str (
373
+ engine->cx (), JS_NewStringCopyN (engine->cx (), RUNTIME_VERSION, strlen (RUNTIME_VERSION)));
374
+ RootedValue version_str_val (engine->cx (), StringValue (version_str));
375
+ if (!JS_SetProperty (engine->cx (), experimental, " sdkVersion" , version_str_val)) {
376
+ return false ;
377
+ }
378
+ if (!engine->define_builtin_module (" fastly:experimental" , experimental_val)) {
368
379
return false ;
369
380
}
370
381
371
382
// TODO(GB): all of the following builtin modules are just placeholder shapes for now
372
- if (!ENGINE ->define_builtin_module (" fastly:body" , env_builtin_val)) {
383
+ if (!engine ->define_builtin_module (" fastly:body" , env_builtin_val)) {
373
384
return false ;
374
385
}
375
- RootedObject cache (ENGINE ->cx (), JS_NewObject (ENGINE ->cx (), nullptr ));
376
- RootedValue cache_val (ENGINE ->cx (), JS::ObjectValue (*cache));
377
- if (!JS_SetProperty (ENGINE ->cx (), cache, " CoreCache" , cache_val)) {
386
+ RootedObject cache (engine ->cx (), JS_NewObject (engine ->cx (), nullptr ));
387
+ RootedValue cache_val (engine ->cx (), JS::ObjectValue (*cache));
388
+ if (!JS_SetProperty (engine ->cx (), cache, " CoreCache" , cache_val)) {
378
389
return false ;
379
390
}
380
- if (!JS_SetProperty (ENGINE ->cx (), cache, " CacheEntry" , cache_val)) {
391
+ if (!JS_SetProperty (engine ->cx (), cache, " CacheEntry" , cache_val)) {
381
392
return false ;
382
393
}
383
- if (!JS_SetProperty (ENGINE ->cx (), cache, " CacheEntry" , cache_val)) {
394
+ if (!JS_SetProperty (engine ->cx (), cache, " CacheEntry" , cache_val)) {
384
395
return false ;
385
396
}
386
- if (!JS_SetProperty (ENGINE ->cx (), cache, " SimpleCache" , cache_val)) {
397
+ if (!JS_SetProperty (engine ->cx (), cache, " SimpleCache" , cache_val)) {
387
398
return false ;
388
399
}
389
- if (!ENGINE ->define_builtin_module (" fastly:cache" , cache_val)) {
400
+ if (!engine ->define_builtin_module (" fastly:cache" , cache_val)) {
390
401
return false ;
391
402
}
392
- if (!ENGINE ->define_builtin_module (" fastly:config-store" , env_builtin_val)) {
403
+ if (!engine ->define_builtin_module (" fastly:config-store" , env_builtin_val)) {
393
404
return false ;
394
405
}
395
- RootedObject device_device (ENGINE ->cx (), JS_NewObject (ENGINE ->cx (), nullptr ));
396
- RootedValue device_device_val (ENGINE ->cx (), JS::ObjectValue (*device_device));
397
- if (!JS_SetProperty (ENGINE ->cx (), device_device, " Device" , device_device_val)) {
406
+ RootedObject device_device (engine ->cx (), JS_NewObject (engine ->cx (), nullptr ));
407
+ RootedValue device_device_val (engine ->cx (), JS::ObjectValue (*device_device));
408
+ if (!JS_SetProperty (engine ->cx (), device_device, " Device" , device_device_val)) {
398
409
return false ;
399
410
}
400
- if (!ENGINE ->define_builtin_module (" fastly:device" , device_device_val)) {
411
+ if (!engine ->define_builtin_module (" fastly:device" , device_device_val)) {
401
412
return false ;
402
413
}
403
- RootedObject dictionary (ENGINE ->cx (), JS_NewObject (ENGINE ->cx (), nullptr ));
404
- RootedValue dictionary_val (ENGINE ->cx (), JS::ObjectValue (*dictionary));
405
- if (!JS_SetProperty (ENGINE ->cx (), dictionary, " Dictionary" , dictionary_val)) {
414
+ RootedObject dictionary (engine ->cx (), JS_NewObject (engine ->cx (), nullptr ));
415
+ RootedValue dictionary_val (engine ->cx (), JS::ObjectValue (*dictionary));
416
+ if (!JS_SetProperty (engine ->cx (), dictionary, " Dictionary" , dictionary_val)) {
406
417
return false ;
407
418
}
408
- if (!ENGINE ->define_builtin_module (" fastly:dictionary" , dictionary_val)) {
419
+ if (!engine ->define_builtin_module (" fastly:dictionary" , dictionary_val)) {
409
420
return false ;
410
421
}
411
- RootedObject edge_rate_limiter (ENGINE ->cx (), JS_NewObject (ENGINE ->cx (), nullptr ));
412
- RootedValue edge_rate_limiter_val (ENGINE ->cx (), JS::ObjectValue (*edge_rate_limiter));
413
- if (!JS_SetProperty (ENGINE ->cx (), edge_rate_limiter, " RateCounter" , edge_rate_limiter_val)) {
422
+ RootedObject edge_rate_limiter (engine ->cx (), JS_NewObject (engine ->cx (), nullptr ));
423
+ RootedValue edge_rate_limiter_val (engine ->cx (), JS::ObjectValue (*edge_rate_limiter));
424
+ if (!JS_SetProperty (engine ->cx (), edge_rate_limiter, " RateCounter" , edge_rate_limiter_val)) {
414
425
return false ;
415
426
}
416
- if (!JS_SetProperty (ENGINE ->cx (), edge_rate_limiter, " PenaltyBox" , edge_rate_limiter_val)) {
427
+ if (!JS_SetProperty (engine ->cx (), edge_rate_limiter, " PenaltyBox" , edge_rate_limiter_val)) {
417
428
return false ;
418
429
}
419
- if (!JS_SetProperty (ENGINE ->cx (), edge_rate_limiter, " EdgeRateLimiter" , edge_rate_limiter_val)) {
430
+ if (!JS_SetProperty (engine ->cx (), edge_rate_limiter, " EdgeRateLimiter" , edge_rate_limiter_val)) {
420
431
return false ;
421
432
}
422
- if (!ENGINE ->define_builtin_module (" fastly:edge-rate-limiter" , edge_rate_limiter_val)) {
433
+ if (!engine ->define_builtin_module (" fastly:edge-rate-limiter" , edge_rate_limiter_val)) {
423
434
return false ;
424
435
}
425
- RootedObject fanout (ENGINE ->cx (), JS_NewObject (ENGINE ->cx (), nullptr ));
426
- RootedValue fanout_val (ENGINE ->cx (), JS::ObjectValue (*fanout));
427
- if (!JS_SetProperty (ENGINE ->cx (), fanout, " createFanoutHandoff" , fanout_val)) {
436
+ RootedObject fanout (engine ->cx (), JS_NewObject (engine ->cx (), nullptr ));
437
+ RootedValue fanout_val (engine ->cx (), JS::ObjectValue (*fanout));
438
+ if (!JS_SetProperty (engine ->cx (), fanout, " createFanoutHandoff" , fanout_val)) {
428
439
return false ;
429
440
}
430
- if (!ENGINE ->define_builtin_module (" fastly:fanout" , fanout_val)) {
441
+ if (!engine ->define_builtin_module (" fastly:fanout" , fanout_val)) {
431
442
return false ;
432
443
}
433
- if (!ENGINE ->define_builtin_module (" fastly:geolocation" , env_builtin_val)) {
444
+ if (!engine ->define_builtin_module (" fastly:geolocation" , env_builtin_val)) {
434
445
return false ;
435
446
}
436
- RootedObject kv_store (ENGINE ->cx (), JS_NewObject (ENGINE ->cx (), nullptr ));
437
- RootedValue kv_store_val (ENGINE ->cx (), JS::ObjectValue (*kv_store));
438
- if (!JS_SetProperty (ENGINE ->cx (), kv_store, " KVStore" , kv_store_val)) {
447
+ RootedObject kv_store (engine ->cx (), JS_NewObject (engine ->cx (), nullptr ));
448
+ RootedValue kv_store_val (engine ->cx (), JS::ObjectValue (*kv_store));
449
+ if (!JS_SetProperty (engine ->cx (), kv_store, " KVStore" , kv_store_val)) {
439
450
return false ;
440
451
}
441
- if (!ENGINE ->define_builtin_module (" fastly:kv-store" , kv_store_val)) {
452
+ if (!engine ->define_builtin_module (" fastly:kv-store" , kv_store_val)) {
442
453
return false ;
443
454
}
444
- if (!ENGINE ->define_builtin_module (" fastly:logger" , env_builtin_val)) {
455
+ if (!engine ->define_builtin_module (" fastly:logger" , env_builtin_val)) {
445
456
return false ;
446
457
}
447
- if (!ENGINE ->define_builtin_module (" fastly:secret-store" , env_builtin_val)) {
458
+ if (!engine ->define_builtin_module (" fastly:secret-store" , env_builtin_val)) {
448
459
return false ;
449
460
}
450
461
@@ -463,8 +474,8 @@ bool install(api::Engine *engine) {
463
474
// options.getExperimentalHighResolutionTimeMethodsEnabled() ? nowfn : end,
464
475
end};
465
476
466
- return JS_DefineFunctions (ENGINE ->cx (), fastly, methods) &&
467
- JS_DefineProperties (ENGINE ->cx (), fastly, Fastly::properties);
477
+ return JS_DefineFunctions (engine ->cx (), fastly, methods) &&
478
+ JS_DefineProperties (engine ->cx (), fastly, Fastly::properties);
468
479
}
469
480
470
481
} // namespace fastly::fastly
0 commit comments