From 41d2f350b99e7e61977a24e9677303010511e5c1 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Thu, 6 May 2021 14:24:27 +0200 Subject: [PATCH 1/3] feat(shell-api): let Symbol global refer to JS builtin MONGOSH-753 --- .../src/stages/transform-maybe-await.ts | 13 ++----------- packages/cli-repl/test/e2e-bson.spec.ts | 6 +++--- packages/i18n/src/locales/en_US.ts | 8 -------- packages/shell-api/src/shell-bson.ts | 3 --- 4 files changed, 5 insertions(+), 25 deletions(-) diff --git a/packages/async-rewriter2/src/stages/transform-maybe-await.ts b/packages/async-rewriter2/src/stages/transform-maybe-await.ts index 74aab6c614..2d503ada90 100644 --- a/packages/async-rewriter2/src/stages/transform-maybe-await.ts +++ b/packages/async-rewriter2/src/stages/transform-maybe-await.ts @@ -46,16 +46,8 @@ export default ({ types: t }: { types: typeof BabelTypes }): babel.PluginObj<{ f // of helpers which are available inside the function. const identifierGroupKey = '@@mongosh.identifierGroup'; - // We fetch the symbol constructor as - // Object.getOwnPropertySymbols(Array.prototype)[0].constructor - // because Symbol refers to BSONSymbol inside the target mongosh context. - // (This is the only mongosh-specific hack in here.) - const symbolConstructor = babel.template.expression(` - Object.getOwnPropertySymbols(Array.prototype)[0].constructor - `)(); - const syntheticPromiseSymbolTemplate = babel.template.statement(` - const SP_IDENTIFIER = SYMBOL_CONSTRUCTOR.for("@@mongosh.syntheticPromise"); + const SP_IDENTIFIER = Symbol.for("@@mongosh.syntheticPromise"); `); const markSyntheticPromiseTemplate = babel.template.statement(` @@ -224,8 +216,7 @@ export default ({ types: t }: { types: typeof BabelTypes }): babel.PluginObj<{ f const commonHelpers = existingIdentifiers ? [] : [ Object.assign( syntheticPromiseSymbolTemplate({ - SP_IDENTIFIER: syntheticPromiseSymbol, - SYMBOL_CONSTRUCTOR: symbolConstructor + SP_IDENTIFIER: syntheticPromiseSymbol }), { [isGeneratedHelper]: true } ), diff --git a/packages/cli-repl/test/e2e-bson.spec.ts b/packages/cli-repl/test/e2e-bson.spec.ts index 07c536337b..62905697a9 100644 --- a/packages/cli-repl/test/e2e-bson.spec.ts +++ b/packages/cli-repl/test/e2e-bson.spec.ts @@ -93,7 +93,7 @@ describe('BSON e2e', function() { NumberInt: NumberInt("32"), NumberLong: NumberLong("64"), Timestamp: new Timestamp(1, 100), - Symbol: new Symbol('abc'), + Symbol: new BSONSymbol('abc'), Code: new Code('abc'), NumberDecimal: NumberDecimal('1'), BinData: BinData(128, 'MTIzNA=='), @@ -282,7 +282,7 @@ describe('BSON e2e', function() { shell.assertNoErrors(); }); it('Symbol prints when created by user', async() => { - const value = 'new Symbol("symbol")'; + const value = 'new BSONSymbol("symbol")'; await shell.writeInputLine(value); await eventually(() => { shell.assertContainsOutput('"symbol"'); @@ -504,7 +504,7 @@ describe('BSON e2e', function() { shell.assertNoErrors(); }); it('Symbol has help when created by user', async() => { - const value = 'new Symbol("1")'; + const value = 'new BSONSymbol("1")'; await shell.writeInputLine(`${value}.help`); await eventually(() => { shell.assertContainsOutput('BSON Class'); diff --git a/packages/i18n/src/locales/en_US.ts b/packages/i18n/src/locales/en_US.ts index dee047ca9d..87c16c2c46 100644 --- a/packages/i18n/src/locales/en_US.ts +++ b/packages/i18n/src/locales/en_US.ts @@ -1752,14 +1752,6 @@ const translations: Catalog = { }, attributes: {} }, - Symbol: { - help: { - description: 'The Symbol BSON Class helper method that constructs a BSONSymbol type. Deprecated since server version 1.6', - link: 'https://mongodb.github.io/node-mongodb-native/3.6/api/Symbol.html', - example: 'Symbol("abc")' - }, - attributes: {} - }, BSONSymbol: { help: { description: 'The Symbol BSON Class. Deprecated since server version 1.6', diff --git a/packages/shell-api/src/shell-bson.ts b/packages/shell-api/src/shell-bson.ts index 34db8930ab..c3c3336da8 100644 --- a/packages/shell-api/src/shell-bson.ts +++ b/packages/shell-api/src/shell-bson.ts @@ -70,9 +70,6 @@ export default function constructShellBson(bson: typeof BSON, printWarning: (msg assertArgsDefinedType([id], [[undefined, 'string']], 'ObjectId'); return new bson.ObjectId(id); }, { prototype: bson.ObjectId.prototype }), - Symbol: Object.assign(function(value = ''): any { - return new bson.BSONSymbol(value); - }, { prototype: bson.BSONSymbol.prototype }), Timestamp: Object.assign(function(low = 0, high = 0): any { assertArgsDefinedType([low, high], ['number', 'number'], 'Timestamp'); return new bson.Timestamp(low, high); From 6dbb82f108da88adad01d44e7d14eb229c65edb0 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Thu, 6 May 2021 15:02:58 +0200 Subject: [PATCH 2/3] fixup --- .../kotlin/com/mongodb/mongosh/MongoShellEvaluator.kt | 7 ++----- packages/shell-api/src/shell-bson.spec.ts | 10 +++++----- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/packages/java-shell/src/main/kotlin/com/mongodb/mongosh/MongoShellEvaluator.kt b/packages/java-shell/src/main/kotlin/com/mongodb/mongosh/MongoShellEvaluator.kt index a18d44653d..43b9c686e9 100644 --- a/packages/java-shell/src/main/kotlin/com/mongodb/mongosh/MongoShellEvaluator.kt +++ b/packages/java-shell/src/main/kotlin/com/mongodb/mongosh/MongoShellEvaluator.kt @@ -31,9 +31,8 @@ internal class MongoShellEvaluator(client: MongoClient, private val context: Mon shellEvaluator = global.getMember("ShellEvaluator").newInstance(shellInternalState, resultHandler()) toShellResultFn = global.getMember("toShellResult") getShellApiTypeFn = global.getMember("getShellApiType") - val jsSymbol = context.bindings["Symbol"]!! shellInternalState.invokeMember("setCtx", context.bindings) - initContext(context.bindings, jsSymbol) + initContext(context.bindings) } private fun resultHandler() = context.jsFun { args -> @@ -47,9 +46,7 @@ internal class MongoShellEvaluator(client: MongoClient, private val context: Mon } } - private fun initContext(bindings: Value, jsSymbol: Value) { - bindings["BSONSymbol"] = bindings["Symbol"] - bindings["Symbol"] = jsSymbol + private fun initContext(bindings: Value) { val date = context.eval("(dateHelper) => function inner() { return dateHelper(new.target !== undefined, ...arguments) }", "dateHelper_script") .execute(ProxyExecutable { args -> dateHelper(args[0].asBoolean(), args.drop(1)) }) date["now"] = ProxyExecutable { System.currentTimeMillis() } diff --git a/packages/shell-api/src/shell-bson.spec.ts b/packages/shell-api/src/shell-bson.spec.ts index 73627e21d2..a97c971cde 100644 --- a/packages/shell-api/src/shell-bson.spec.ts +++ b/packages/shell-api/src/shell-bson.spec.ts @@ -126,24 +126,24 @@ describe('Shell BSON', () => { expect.fail('Expecting error, nothing thrown'); }); }); - describe('Symbol', () => { + describe('BSONSymbol', () => { it('without new', () => { - const s = shellBson.Symbol('5ebbe8e2905bb493d6981b6b'); + const s = shellBson.BSONSymbol('5ebbe8e2905bb493d6981b6b'); expect(s._bsontype).to.equal('Symbol'); expect(s.toString()).to.equal('5ebbe8e2905bb493d6981b6b'); }); it('with new', () => { - const s = new (shellBson.Symbol as any)('5ebbe8e2905bb493d6981b6b'); + const s = new (shellBson.BSONSymbol as any)('5ebbe8e2905bb493d6981b6b'); expect(s._bsontype).to.equal('Symbol'); expect(s.toString()).to.equal('5ebbe8e2905bb493d6981b6b'); }); it('has help and other metadata', async() => { - const s = shellBson.Symbol('5ebbe8e2905bb493d6981b6b'); + const s = shellBson.BSONSymbol('5ebbe8e2905bb493d6981b6b'); expect((await toShellResult(s.help)).type).to.equal('Help'); expect((await toShellResult(s.help())).type).to.equal('Help'); }); it('constructs with default args 1', () => { - const s = shellBson.Symbol(); + const s = shellBson.BSONSymbol(); expect(s.toString()).to.equal(''); }); }); From a3bdbd9531eb9351176866f6a30f5d156985f8f7 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Thu, 6 May 2021 15:43:58 +0200 Subject: [PATCH 3/3] fixup --- packages/shell-api/src/shell-bson.spec.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/packages/shell-api/src/shell-bson.spec.ts b/packages/shell-api/src/shell-bson.spec.ts index a97c971cde..4aec3c4190 100644 --- a/packages/shell-api/src/shell-bson.spec.ts +++ b/packages/shell-api/src/shell-bson.spec.ts @@ -142,10 +142,6 @@ describe('Shell BSON', () => { expect((await toShellResult(s.help)).type).to.equal('Help'); expect((await toShellResult(s.help())).type).to.equal('Help'); }); - it('constructs with default args 1', () => { - const s = shellBson.BSONSymbol(); - expect(s.toString()).to.equal(''); - }); }); describe('Timestamp', () => { it('without new', () => {