1
1
import harden from '@agoric/harden' ;
2
- import RE2 from 're2' ;
3
-
4
2
import * as c from './constants' ;
5
3
6
- const { create, defineProperties, entries, fromEntries,
7
- getOwnPropertyDescriptors, getPrototypeOf } = Object ;
4
+ // We'd like to import this, but RE2 is cjs
5
+ const RE2 = require ( 're2' ) ;
6
+
7
+ const {
8
+ create,
9
+ defineProperties,
10
+ entries,
11
+ fromEntries,
12
+ getOwnPropertyDescriptors,
13
+ getPrototypeOf,
14
+ } = Object ;
8
15
9
16
export function makeMeteringEndowments (
10
17
meter ,
@@ -13,15 +20,15 @@ export function makeMeteringEndowments(
13
20
overrideMeterId = c . DEFAULT_METER_ID ,
14
21
) {
15
22
const wrapped = new WeakMap ( ) ;
23
+ wrapped . set ( meter , meter ) ;
16
24
const meterId = overrideMeterId ;
17
25
18
26
const wrapDescriptor = desc =>
19
- fromEntries ( entries ( desc ) . map ( ( [ k , v ] ) =>
20
- [ k , wrap ( v ) ]
21
- ) ) ;
27
+ fromEntries ( entries ( desc ) . map ( ( [ k , v ] ) => [ k , wrap ( v ) ] ) ) ;
22
28
29
+ const shadowedRegexp = globalsToShadow . RegExp ;
23
30
function wrap ( target ) {
24
- if ( target === globalsToShadow . RegExp ) {
31
+ if ( shadowedRegexp !== undefined && target === shadowedRegexp ) {
25
32
// Replace the RegExp object with RE2.
26
33
target = RE2 ;
27
34
}
@@ -70,19 +77,21 @@ export function makeMeteringEndowments(
70
77
wrapped . set ( wrapper , wrapper ) ;
71
78
72
79
// Assign the wrapped descriptors to the wrapper.
73
- const descs = fromEntries ( entries ( getOwnPropertyDescriptors ( target ) )
74
- . map ( ( [ k , v ] ) => [ k , wrapDescriptor ( v ) ] ) ) ;
80
+ const descs = fromEntries (
81
+ entries ( getOwnPropertyDescriptors ( target ) ) . map ( ( [ k , v ] ) => [
82
+ k ,
83
+ wrapDescriptor ( v ) ,
84
+ ] ) ,
85
+ ) ;
75
86
defineProperties ( wrapper , descs ) ;
76
87
return wrapper ;
77
88
}
78
89
79
90
// Shadow the wrapped globals with the wrapped endowments.
80
91
const shadowDescs = create ( null ) ;
81
- entries ( getOwnPropertyDescriptors ( globalsToShadow ) ) . forEach (
82
- ( [ p , desc ] ) => {
83
- shadowDescs [ p ] = wrapDescriptor ( desc ) ;
84
- } ,
85
- ) ;
92
+ entries ( getOwnPropertyDescriptors ( globalsToShadow ) ) . forEach ( ( [ p , desc ] ) => {
93
+ shadowDescs [ p ] = wrapDescriptor ( desc ) ;
94
+ } ) ;
86
95
87
96
entries ( getOwnPropertyDescriptors ( endowments ) ) . forEach ( ( [ p , desc ] ) => {
88
97
// We wrap the endowment descriptors, too.
@@ -100,5 +109,6 @@ export function makeMeteringEndowments(
100
109
} ;
101
110
102
111
// Package up these endowments as an object.
103
- return create ( null , shadowDescs ) ;
112
+ const e = create ( null , shadowDescs ) ;
113
+ return e ;
104
114
}
0 commit comments