diff --git a/Dockerfile b/Dockerfile index afe5c1f43bf05..6c58b9e6f8ec6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -32,15 +32,18 @@ ENV DEBIAN_FRONTEND=noninteractive # Both archive.ubuntu.com and azure.archive.ubuntu.com have intermittently # timed out from inside the GitHub-hosted docker-buildx network at different -# times. Prefer Azure (faster on Azure-hosted runners) but fall back to the -# canonical mirror if `apt-get update` can't reach it. arm64 uses -# ports.ubuntu.com which has been reachable, so leave it alone. -RUN sed -i 's|http://archive.ubuntu.com/ubuntu|http://azure.archive.ubuntu.com/ubuntu|g' /etc/apt/sources.list +# times, sometimes halfway through a step. Give apt both through its mirror +# method -- Azure first (faster on Azure-hosted runners), the canonical mirror +# as fallback -- so every individual fetch that fails on one, in any later +# `apt-get` too (llvm.sh, cmake, gcc), is retried on the other; and retry +# transient errors. arm64 uses ports.ubuntu.com which has been reachable, so +# its sources.list is left alone (the sed matches nothing there). +RUN printf 'http://azure.archive.ubuntu.com/ubuntu/\tpriority:1\nhttp://archive.ubuntu.com/ubuntu/\tpriority:2\n' > /etc/apt/mirrors.txt \ + && sed -i 's|http://archive.ubuntu.com/ubuntu/\?|mirror+file:/etc/apt/mirrors.txt|g' /etc/apt/sources.list \ + && printf 'Acquire::Retries "5";\nAcquire::http::Timeout "30";\nAcquire::https::Timeout "30";\n' > /etc/apt/apt.conf.d/80-retries # Install basic build dependencies -RUN ( apt-get update || \ - ( sed -i 's|http://azure.archive.ubuntu.com/ubuntu|http://archive.ubuntu.com/ubuntu|g' /etc/apt/sources.list && apt-get update ) \ - ) && apt-get install -y \ +RUN apt-get update && apt-get install -y \ wget \ curl \ git \ diff --git a/Source/JavaScriptCore/bytecode/BytecodeList.rb b/Source/JavaScriptCore/bytecode/BytecodeList.rb index 4552d248a9f80..c24590acd85e2 100644 --- a/Source/JavaScriptCore/bytecode/BytecodeList.rb +++ b/Source/JavaScriptCore/bytecode/BytecodeList.rb @@ -573,6 +573,7 @@ }, metadata: { getPutInfo: GetPutInfo, + interceptorOffset: unsigned, # InterceptedGlobalProperty: the property's offset in the interceptor (operand is then the global's own variable slot, or 0); fills the padding before the union _: { structureID: WriteBarrierStructureID, watchpointSet: WatchpointSet.*, diff --git a/Source/JavaScriptCore/bytecode/CodeBlock.cpp b/Source/JavaScriptCore/bytecode/CodeBlock.cpp index d8915876a702a..f08ef057b7f10 100644 --- a/Source/JavaScriptCore/bytecode/CodeBlock.cpp +++ b/Source/JavaScriptCore/bytecode/CodeBlock.cpp @@ -597,7 +597,7 @@ bool CodeBlock::finishCreation(VM& vm, ScriptExecutable* ownerExecutable, Unlink metadata.m_symbolTable.set(vm, this, op.lexicalEnvironment->symbolTable()); } else if (JSScope* constantScope = JSScope::constantScopeForCodeBlock(op.type, this)) { metadata.m_constantScope.set(vm, this, constantScope); - if (op.type == GlobalProperty || op.type == GlobalPropertyWithVarInjectionChecks) + if (isGlobalPropertyResolveType(op.type)) metadata.m_globalLexicalBindingEpoch = m_globalObject->globalLexicalBindingEpoch(); } else metadata.m_globalObject.clear(); @@ -3312,7 +3312,7 @@ void CodeBlock::notifyLexicalBindingUpdate() auto bytecode = instruction->as(); auto& metadata = bytecode.metadata(this); ResolveType originalResolveType = metadata.m_resolveType; - if (originalResolveType == GlobalProperty || originalResolveType == GlobalPropertyWithVarInjectionChecks) { + if (isGlobalPropertyResolveType(originalResolveType)) { const Identifier& ident = identifier(bytecode.m_var); if (isShadowed(ident.impl())) metadata.m_globalLexicalBindingEpoch = 0; diff --git a/Source/JavaScriptCore/dfg/DFGAbstractInterpreterInlines.h b/Source/JavaScriptCore/dfg/DFGAbstractInterpreterInlines.h index 5bfd9ab3c2c78..975cfd92af1d6 100644 --- a/Source/JavaScriptCore/dfg/DFGAbstractInterpreterInlines.h +++ b/Source/JavaScriptCore/dfg/DFGAbstractInterpreterInlines.h @@ -5230,6 +5230,11 @@ bool AbstractInterpreter::executeEffects(unsigned clobberLimi break; } + case CheckNotCellOperand: + // "Anything but this cell" isn't expressible as an AbstractValue; the constant folder removes the check when the + // child provably isn't it. + break; + case AssertNotEmpty: case CheckNotEmpty: { AbstractValue& value = forNode(node->child1()); diff --git a/Source/JavaScriptCore/dfg/DFGByteCodeParser.cpp b/Source/JavaScriptCore/dfg/DFGByteCodeParser.cpp index 489c56adb37d4..68ffe1c27a263 100644 --- a/Source/JavaScriptCore/dfg/DFGByteCodeParser.cpp +++ b/Source/JavaScriptCore/dfg/DFGByteCodeParser.cpp @@ -6632,6 +6632,9 @@ bool NODELETE ByteCodeParser::needsDynamicLookup(ResolveType type, OpcodeID opco case ClosureVar: case ResolvedClosureVar: case ModuleVar: +#if USE(BUN_JSC_ADDITIONS) + case InterceptedGlobalProperty: +#endif return false; case UnresolvedProperty: @@ -10060,6 +10063,9 @@ void ByteCodeParser::parseBlock(unsigned limit) case GlobalVarWithVarInjectionChecks: case GlobalLexicalVar: case GlobalLexicalVarWithVarInjectionChecks: +#if USE(BUN_JSC_ADDITIONS) + case InterceptedGlobalProperty: +#endif constantScope = metadata.m_constantScope.get(); break; case ModuleVar: @@ -10085,7 +10091,7 @@ void ByteCodeParser::parseBlock(unsigned limit) if (needsVarInjectionChecks(resolveType)) m_graph.watchpoints().addLazily(m_inlineStackTop->m_codeBlock->globalObject()->varInjectionWatchpointSet()); - if (resolveType == GlobalProperty || resolveType == GlobalPropertyWithVarInjectionChecks) { + if (isGlobalPropertyResolveType(resolveType)) { JSGlobalObject* globalObject = m_inlineStackTop->m_codeBlock->globalObject(); unsigned identifierNumber = m_inlineStackTop->m_identifierRemap[bytecode.m_var]; if (!m_graph.watchGlobalProperty(globalObject, identifierNumber)) @@ -10098,7 +10104,11 @@ void ByteCodeParser::parseBlock(unsigned limit) case GlobalPropertyWithVarInjectionChecks: case GlobalVarWithVarInjectionChecks: case GlobalLexicalVar: - case GlobalLexicalVarWithVarInjectionChecks: { + case GlobalLexicalVarWithVarInjectionChecks: +#if USE(BUN_JSC_ADDITIONS) + case InterceptedGlobalProperty: +#endif + { RELEASE_ASSERT(constantScope); RELEASE_ASSERT(constantScope == JSScope::constantScopeForCodeBlock(resolveType, m_inlineStackTop->m_codeBlock)); set(bytecode.m_dst, weakJSConstant(constantScope)); @@ -10178,7 +10188,7 @@ void ByteCodeParser::parseBlock(unsigned limit) resolveType = getPutInfo.resolveType(); if (resolveType == GlobalVar || resolveType == GlobalVarWithVarInjectionChecks || resolveType == GlobalLexicalVar || resolveType == GlobalLexicalVarWithVarInjectionChecks) watchpoints = metadata.m_watchpointSet; - else if (resolveType == GlobalProperty || resolveType == GlobalPropertyWithVarInjectionChecks) + else if (isGlobalPropertyResolveType(resolveType)) structure = metadata.m_structureID.get(); operand = metadata.m_operand; } @@ -10224,6 +10234,35 @@ void ByteCodeParser::parseBlock(unsigned limit) set(bytecode.m_dst, result); break; } +#if USE(BUN_JSC_ADDITIONS) + case InterceptedGlobalProperty: { + if (!m_graph.watchGlobalProperty(globalObject, identifierNumber)) + addToGraph(ForceOSRExit); + + SpeculatedType prediction = getPrediction(); + CacheableIdentifier identifier = CacheableIdentifier::createFromIdentifierOwnedByCodeBlock(m_inlineStackTop->m_profiledBlock, uid); + JSObject* interceptor = globalObject->globalScopeInterceptor(); + + // `structure`, if the baseline cache formed, is a structure of the interceptor under which the name is + // a plain own data property of it, which the global's getOwnPropertySlot returns first: load it from + // the interceptor directly. Anything else takes the generic path (the name may not exist at all, so + // one that can throw the ReferenceError). + GetByStatus status = structure ? GetByStatus::computeFor(m_inlineStackTop->m_profiledBlock, m_currentIndex, globalObject, structure, identifier, GetByStatus::LookupMode::Normal) : GetByStatus(GetByStatus::LikelyTakesSlowPath); + if (status.state() != GetByStatus::Simple || status.numVariants() != 1 || status[0].structureSet().size() != 1 || status[0].conditionSet().size()) { + set(bytecode.m_dst, addToGraph(GetDynamicVar, OpInfo(makeDynamicVarOpInfo(identifierNumber, getPutInfo.operand())), OpInfo(prediction), get(bytecode.m_scope))); + break; + } + + Node* base = weakJSConstant(interceptor); + Node* result = load(prediction, base, base, identifierNumber, status[0]); + // The global's getOwnPropertySlot substitutes its globalThis for a property holding the interceptor + // itself; leave that to it. + addToGraph(CheckNotCellOperand, OpInfo(m_graph.freeze(interceptor)), result); + addToGraph(Phantom, get(bytecode.m_scope)); + set(bytecode.m_dst, result); + break; + } +#endif case GlobalVar: case GlobalVarWithVarInjectionChecks: case GlobalLexicalVar: @@ -10365,15 +10404,21 @@ void ByteCodeParser::parseBlock(unsigned limit) Structure* structure = nullptr; WatchpointSet* watchpoints = nullptr; uintptr_t operand; +#if USE(BUN_JSC_ADDITIONS) + unsigned interceptorOffset = 0; +#endif { ConcurrentJSLocker locker(m_inlineStackTop->m_profiledBlock->m_lock); getPutInfo = metadata.m_getPutInfo; resolveType = getPutInfo.resolveType(); if (resolveType == GlobalVar || resolveType == GlobalVarWithVarInjectionChecks || resolveType == ResolvedClosureVar || resolveType == GlobalLexicalVar || resolveType == GlobalLexicalVarWithVarInjectionChecks) watchpoints = metadata.m_watchpointSet; - else if (resolveType == GlobalProperty || resolveType == GlobalPropertyWithVarInjectionChecks) + else if (isGlobalPropertyResolveType(resolveType)) structure = metadata.m_structureID.get(); operand = metadata.m_operand; +#if USE(BUN_JSC_ADDITIONS) + interceptorOffset = metadata.m_interceptorOffset; +#endif } JSGlobalObject* globalObject = m_inlineStackTop->m_codeBlock->globalObject(); @@ -10408,6 +10453,33 @@ void ByteCodeParser::parseBlock(unsigned limit) addToGraph(Phantom, get(bytecode.m_scope)); break; } +#if USE(BUN_JSC_ADDITIONS) + case InterceptedGlobalProperty: { + if (!m_graph.watchGlobalProperty(globalObject, identifierNumber)) + addToGraph(ForceOSRExit); + JSObject* interceptor = globalObject->globalScopeInterceptor(); + + // A baseline cache means: the name is a plain, writable own data property of the interceptor at + // `interceptorOffset` under `structure`, and `operand`, if non-zero, is the global's own variable slot for + // it, which the global's put() also stores to. Anything else takes the generic path (the name may not exist at + // all, so one that can throw the strict-mode ReferenceError). + PutByStatus status = (uid && structure) ? PutByStatus::computeFor(m_inlineStackTop->m_profiledBlock, m_currentIndex, globalObject, structure, CacheableIdentifier::createFromIdentifierOwnedByCodeBlock(m_inlineStackTop->m_profiledBlock, uid), false, PrivateFieldPutKind::none()) : PutByStatus(PutByStatus::LikelyTakesSlowPath); + if (status.numVariants() != 1 || status[0].kind() != PutByVariant::Replace || status[0].structure().size() != 1 || status[0].offset() != static_cast(interceptorOffset)) { + ASSERT(identifierNumber != UINT_MAX); + addToGraph(PutDynamicVar, OpInfo(makeDynamicVarOpInfo(identifierNumber, getPutInfo.operand())), OpInfo(getPutInfo.ecmaMode()), get(bytecode.m_scope), get(bytecode.m_value)); + break; + } + Node* base = weakJSConstant(interceptor); + Node* variableScope = operand ? weakJSConstant(globalObject) : nullptr; // ahead of replace(), after which exit is invalid + Node* valueNode = get(bytecode.m_value); + replace(base, identifierNumber, status[0], valueNode); + if (variableScope) + addToGraph(PutGlobalVariable, OpInfo(operand), variableScope, valueNode); + // Keep scope alive until after put. + addToGraph(Phantom, get(bytecode.m_scope)); + break; + } +#endif case GlobalLexicalVar: case GlobalLexicalVarWithVarInjectionChecks: case GlobalVar: diff --git a/Source/JavaScriptCore/dfg/DFGClobberize.h b/Source/JavaScriptCore/dfg/DFGClobberize.h index 27bd50bfbb555..3819e6fb8454a 100644 --- a/Source/JavaScriptCore/dfg/DFGClobberize.h +++ b/Source/JavaScriptCore/dfg/DFGClobberize.h @@ -552,6 +552,10 @@ void clobberize(Graph& graph, Node* node, const ReadFunctor& read, const WriteFu def(PureValue(CheckIsConstant, AdjacencyList(AdjacencyList::Fixed, node->child1()), node->constant())); return; + case CheckNotCellOperand: + def(PureValue(CheckNotCellOperand, AdjacencyList(AdjacencyList::Fixed, node->child1()), node->cellOperand())); + return; + case CheckNotEmpty: def(PureValue(CheckNotEmpty, AdjacencyList(AdjacencyList::Fixed, node->child1()))); return; diff --git a/Source/JavaScriptCore/dfg/DFGCloneHelper.h b/Source/JavaScriptCore/dfg/DFGCloneHelper.h index b22b25e4de906..dddf8df8ec3bf 100644 --- a/Source/JavaScriptCore/dfg/DFGCloneHelper.h +++ b/Source/JavaScriptCore/dfg/DFGCloneHelper.h @@ -184,6 +184,7 @@ BasicBlock* CloneHelper::cloneBlock(BasicBlock* const block, const CustomizeSucc CLONE_STATUS(CheckBadValue, Common) \ CLONE_STATUS(CheckIdent, Common) \ CLONE_STATUS(CheckIsConstant, Common) \ + CLONE_STATUS(CheckNotCellOperand, Common) \ CLONE_STATUS(CheckNotEmpty, Common) \ CLONE_STATUS(CheckStructure, Common) \ CLONE_STATUS(CheckStructureOrEmpty, Common) \ diff --git a/Source/JavaScriptCore/dfg/DFGConstantFoldingPhase.cpp b/Source/JavaScriptCore/dfg/DFGConstantFoldingPhase.cpp index d8b1d6943d412..960a760bdc66f 100644 --- a/Source/JavaScriptCore/dfg/DFGConstantFoldingPhase.cpp +++ b/Source/JavaScriptCore/dfg/DFGConstantFoldingPhase.cpp @@ -460,6 +460,15 @@ class ConstantFoldingPhase : public Phase { break; } + case CheckNotCellOperand: { + AbstractValue& value = m_state.forNode(node->child1()); + if ((value.m_type & SpecCell) && !(value.value() && value.value() != node->cellOperand()->value())) + break; + node->remove(m_graph); + eliminated = true; + break; + } + case AssertNotEmpty: case CheckNotEmpty: { if (m_state.forNode(node->child1()).m_type & SpecEmpty) diff --git a/Source/JavaScriptCore/dfg/DFGDoesGC.cpp b/Source/JavaScriptCore/dfg/DFGDoesGC.cpp index 74aa84ec57554..58dd68264db8d 100644 --- a/Source/JavaScriptCore/dfg/DFGDoesGC.cpp +++ b/Source/JavaScriptCore/dfg/DFGDoesGC.cpp @@ -126,6 +126,7 @@ bool doesGC(Graph& graph, Node* node) case GetGlobalLexicalVariable: case PutGlobalVariable: case CheckIsConstant: + case CheckNotCellOperand: case CheckNotEmpty: case AssertNotEmpty: case CheckIdent: diff --git a/Source/JavaScriptCore/dfg/DFGFixupPhase.cpp b/Source/JavaScriptCore/dfg/DFGFixupPhase.cpp index 8f13e40e26899..fb88d4f61bcc0 100644 --- a/Source/JavaScriptCore/dfg/DFGFixupPhase.cpp +++ b/Source/JavaScriptCore/dfg/DFGFixupPhase.cpp @@ -2616,6 +2616,9 @@ class FixupPhase : public Phase { break; } + case CheckNotCellOperand: + break; + case ObjectCreate: { if (node->child1()->shouldSpeculateObject()) { fixEdge(node->child1()); diff --git a/Source/JavaScriptCore/dfg/DFGNode.h b/Source/JavaScriptCore/dfg/DFGNode.h index 5697f8a5203fa..5135df76939cb 100644 --- a/Source/JavaScriptCore/dfg/DFGNode.h +++ b/Source/JavaScriptCore/dfg/DFGNode.h @@ -612,6 +612,7 @@ struct Node { case CheckArrayOrEmpty: case CheckDetached: case CheckIsConstant: + case CheckNotCellOperand: case CheckNotEmpty: case CheckBadValue: case CheckInBounds: @@ -2263,6 +2264,8 @@ struct Node { switch (op()) { case CheckIsConstant: return isCell(child1().useKind()); + case CheckNotCellOperand: + return true; case OverridesHasInstance: case NewFunction: case NewGeneratorFunction: diff --git a/Source/JavaScriptCore/dfg/DFGNodeType.h b/Source/JavaScriptCore/dfg/DFGNodeType.h index 983bfd526e8b9..9b58f1f1e10f8 100644 --- a/Source/JavaScriptCore/dfg/DFGNodeType.h +++ b/Source/JavaScriptCore/dfg/DFGNodeType.h @@ -307,6 +307,8 @@ namespace JSC { namespace DFG { macro(SetRegExpObjectLastIndex, NodeMustGenerate) \ macro(RecordRegExpCachedResult, NodeMustGenerate | NodeHasVarArgs) \ macro(CheckIsConstant, NodeMustGenerate) \ + /* OSR exit if the (untyped) child is the cell operand. Used by Bun's InterceptedGlobalProperty. */\ + macro(CheckNotCellOperand, NodeMustGenerate) \ macro(CheckNotEmpty, NodeMustGenerate) \ macro(AssertNotEmpty, NodeMustGenerate) \ macro(CheckBadValue, NodeMustGenerate) \ diff --git a/Source/JavaScriptCore/dfg/DFGOperations.cpp b/Source/JavaScriptCore/dfg/DFGOperations.cpp index 3792e46975b78..9dcb0a2515d45 100644 --- a/Source/JavaScriptCore/dfg/DFGOperations.cpp +++ b/Source/JavaScriptCore/dfg/DFGOperations.cpp @@ -5968,26 +5968,27 @@ JSC_DEFINE_JIT_OPERATION(operationGetDynamicVar, EncodedJSValue, (JSGlobalObject JITOperationPrologueCallFrameTracer tracer(vm, callFrame); auto scope = DECLARE_THROW_SCOPE(vm); - Identifier ident = Identifier::fromUid(vm, impl); - OPERATION_RETURN(scope, JSValue::encode(jsScope->getPropertySlot(globalObject, ident, [&] (bool found, PropertySlot& slot) -> JSValue { + // The Identifier (a ref of the uid) is only needed to build an error. + PropertyName propertyName(impl); + OPERATION_RETURN(scope, JSValue::encode(jsScope->getPropertySlot(globalObject, propertyName, [&] (bool found, PropertySlot& slot) -> JSValue { if (!found) { GetPutInfo getPutInfo(getPutInfoBits); if (getPutInfo.resolveMode() == ThrowIfNotFound) - throwException(globalObject, scope, createUndefinedVariableError(globalObject, ident)); + throwException(globalObject, scope, createUndefinedVariableError(globalObject, Identifier::fromUid(vm, impl))); return jsUndefined(); } if (jsScope->isGlobalLexicalEnvironment()) { // When we can't statically prove we need a TDZ check, we must perform the check on the slow path. - JSValue result = slot.getValue(globalObject, ident); + JSValue result = slot.getValue(globalObject, propertyName); if (result == jsTDZValue()) { - throwException(globalObject, scope, createTDZError(globalObject, ident.string())); + throwException(globalObject, scope, createTDZError(globalObject, Identifier::fromUid(vm, impl).string())); return jsUndefined(); } return result; } - return slot.getValue(globalObject, ident); + return slot.getValue(globalObject, propertyName); }))); } @@ -5995,30 +5996,31 @@ ALWAYS_INLINE static void putDynamicVar(JSGlobalObject* globalObject, VM& vm, JS { auto throwScope = DECLARE_THROW_SCOPE(vm); - const Identifier& ident = Identifier::fromUid(vm, impl); + // The Identifier (a ref of the uid) is only needed to build an error. + PropertyName propertyName(impl); GetPutInfo getPutInfo(getPutInfoBits); - bool hasProperty = scope->hasProperty(globalObject, ident); + bool hasProperty = scope->hasProperty(globalObject, propertyName); RETURN_IF_EXCEPTION(throwScope, void()); if (hasProperty && scope->isGlobalLexicalEnvironment() && !isInitialization(getPutInfo.initializationMode())) { // When we can't statically prove we need a TDZ check, we must perform the check on the slow path. PropertySlot slot(scope, PropertySlot::InternalMethodType::Get); - JSGlobalLexicalEnvironment::getOwnPropertySlot(scope, globalObject, ident, slot); - if (slot.getValue(globalObject, ident) == jsTDZValue()) { - throwException(globalObject, throwScope, createTDZError(globalObject, ident.string())); + JSGlobalLexicalEnvironment::getOwnPropertySlot(scope, globalObject, propertyName, slot); + if (slot.getValue(globalObject, propertyName) == jsTDZValue()) { + throwException(globalObject, throwScope, createTDZError(globalObject, Identifier::fromUid(vm, impl).string())); return; } } if (getPutInfo.resolveMode() == ThrowIfNotFound && !hasProperty) { - throwException(globalObject, throwScope, createUndefinedVariableError(globalObject, ident)); + throwException(globalObject, throwScope, createUndefinedVariableError(globalObject, Identifier::fromUid(vm, impl))); return; } PutPropertySlot slot(scope, isStrictMode, PutPropertySlot::UnknownContext, isInitialization(getPutInfo.initializationMode())); throwScope.release(); - scope->methodTable()->put(scope, globalObject, ident, JSValue::decode(value), slot); + scope->methodTable()->put(scope, globalObject, propertyName, JSValue::decode(value), slot); } JSC_DEFINE_JIT_OPERATION(operationPutDynamicVarStrict, void, (JSGlobalObject* globalObject, JSObject* jsScope, EncodedJSValue value, UniquedStringImpl* impl, unsigned getPutInfoBits)) diff --git a/Source/JavaScriptCore/dfg/DFGPredictionPropagationPhase.cpp b/Source/JavaScriptCore/dfg/DFGPredictionPropagationPhase.cpp index 8b529d7d4912e..67937112f6798 100644 --- a/Source/JavaScriptCore/dfg/DFGPredictionPropagationPhase.cpp +++ b/Source/JavaScriptCore/dfg/DFGPredictionPropagationPhase.cpp @@ -1811,6 +1811,7 @@ class PredictionPropagationPhase : public Phase { case EnqueueAsyncGeneratorDriver: case CheckStructure: case CheckIsConstant: + case CheckNotCellOperand: case CheckNotEmpty: case AssertNotEmpty: case CheckIdent: diff --git a/Source/JavaScriptCore/dfg/DFGSafeToExecute.h b/Source/JavaScriptCore/dfg/DFGSafeToExecute.h index 73631d719467a..a4e1b52cd7015 100644 --- a/Source/JavaScriptCore/dfg/DFGSafeToExecute.h +++ b/Source/JavaScriptCore/dfg/DFGSafeToExecute.h @@ -254,6 +254,7 @@ bool safeToExecute(AbstractStateType& state, Graph& graph, Node* node, bool igno case GetGlobalVar: case GetGlobalLexicalVariable: case CheckIsConstant: + case CheckNotCellOperand: case CheckNotEmpty: case AssertNotEmpty: case CheckIdent: diff --git a/Source/JavaScriptCore/dfg/DFGSpeculativeJIT.cpp b/Source/JavaScriptCore/dfg/DFGSpeculativeJIT.cpp index c788bcf16598c..3123771b80626 100644 --- a/Source/JavaScriptCore/dfg/DFGSpeculativeJIT.cpp +++ b/Source/JavaScriptCore/dfg/DFGSpeculativeJIT.cpp @@ -10600,6 +10600,14 @@ void SpeculativeJIT::compileCheckIsConstant(Node* node) noResult(node); } +void SpeculativeJIT::compileCheckNotCellOperand(Node* node) +{ + JSValueOperand operand(this, node->child1()); + JSValueRegs regs = operand.jsValueRegs(); + speculationCheck(BadCache, regs, node->child1(), branchLinkableConstant(Equal, regs.payloadGPR(), LinkableConstant(*this, node->cellOperand()->cell()))); + noResult(node); +} + void SpeculativeJIT::compileCheckNotEmpty(Node* node) { JSValueOperand operand(this, node->child1()); diff --git a/Source/JavaScriptCore/dfg/DFGSpeculativeJIT.h b/Source/JavaScriptCore/dfg/DFGSpeculativeJIT.h index d3af9cec15ea9..2cecf9924d688 100644 --- a/Source/JavaScriptCore/dfg/DFGSpeculativeJIT.h +++ b/Source/JavaScriptCore/dfg/DFGSpeculativeJIT.h @@ -1597,6 +1597,7 @@ class SpeculativeJIT : public JITCompiler { void compileIsConstructor(Node*); void compileTypeOf(Node*); void compileCheckIsConstant(Node*); + void compileCheckNotCellOperand(Node*); void compileCheckNotEmpty(Node*); void compileCheckStructure(Node*); void emitStructureCheck(Node*, GPRReg cellGPR, GPRReg tempGPR); diff --git a/Source/JavaScriptCore/dfg/DFGSpeculativeJIT64.cpp b/Source/JavaScriptCore/dfg/DFGSpeculativeJIT64.cpp index 48eb76d01fe90..bb095cd3f18c4 100644 --- a/Source/JavaScriptCore/dfg/DFGSpeculativeJIT64.cpp +++ b/Source/JavaScriptCore/dfg/DFGSpeculativeJIT64.cpp @@ -5124,6 +5124,11 @@ void SpeculativeJIT::compile(Node* node) break; } + case CheckNotCellOperand: { + compileCheckNotCellOperand(node); + break; + } + case CheckNotEmpty: { compileCheckNotEmpty(node); break; diff --git a/Source/JavaScriptCore/ftl/FTLCapabilities.cpp b/Source/JavaScriptCore/ftl/FTLCapabilities.cpp index 8b4ccb9aece3a..7521effcb35d6 100644 --- a/Source/JavaScriptCore/ftl/FTLCapabilities.cpp +++ b/Source/JavaScriptCore/ftl/FTLCapabilities.cpp @@ -166,6 +166,7 @@ inline CapabilityLevel canCompile(DFG::Node* node) case StringCharAt: case StringLocaleCompare: case CheckIsConstant: + case CheckNotCellOperand: case CheckBadValue: case CheckNotEmpty: case AssertNotEmpty: diff --git a/Source/JavaScriptCore/ftl/FTLLowerDFGToB3.cpp b/Source/JavaScriptCore/ftl/FTLLowerDFGToB3.cpp index 07aa95b3c9a70..eddc9fd6d407d 100644 --- a/Source/JavaScriptCore/ftl/FTLLowerDFGToB3.cpp +++ b/Source/JavaScriptCore/ftl/FTLLowerDFGToB3.cpp @@ -969,6 +969,9 @@ class LowerDFGToB3 { case CheckIsConstant: compileCheckIsConstant(); break; + case CheckNotCellOperand: + compileCheckNotCellOperand(); + break; case CheckNotEmpty: compileCheckNotEmpty(); break; @@ -4295,6 +4298,12 @@ class LowerDFGToB3 { } } + void compileCheckNotCellOperand() + { + LValue value = lowJSValue(m_node->child1()); + speculate(BadCache, jsValueValue(value), m_node->child1().node(), m_out.equal(value, weakPointer(m_node->cellOperand()->cell()))); + } + void compileCheckIsConstant() { if (m_node->child1().useKind() == CellUse) { diff --git a/Source/JavaScriptCore/jit/JITOperations.cpp b/Source/JavaScriptCore/jit/JITOperations.cpp index 0d72367c10051..4dfbae121789d 100644 --- a/Source/JavaScriptCore/jit/JITOperations.cpp +++ b/Source/JavaScriptCore/jit/JITOperations.cpp @@ -4605,14 +4605,22 @@ JSC_DEFINE_JIT_OPERATION(operationResolveScopeForBaseline, EncodedJSValue, (JSGl case GlobalProperty: case GlobalPropertyWithVarInjectionChecks: case UnresolvedProperty: - case UnresolvedPropertyWithVarInjectionChecks: { + case UnresolvedPropertyWithVarInjectionChecks: +#if USE(BUN_JSC_ADDITIONS) + case InterceptedGlobalProperty: +#endif + { if (resolvedScope->isGlobalObject()) { JSGlobalObject* globalObject = uncheckedDowncast(resolvedScope); bool hasProperty = globalObject->hasProperty(globalObject, ident); OPERATION_RETURN_IF_EXCEPTION(scope, encodedJSValue()); if (hasProperty) { ConcurrentJSLocker locker(codeBlock->m_lock); +#if USE(BUN_JSC_ADDITIONS) + metadata.m_resolveType = JSScope::globalPropertyResolveType(globalObject, resolveType); +#else metadata.m_resolveType = needsVarInjectionChecks(resolveType) ? GlobalPropertyWithVarInjectionChecks : GlobalProperty; +#endif metadata.m_globalObject.set(vm, codeBlock, globalObject); metadata.m_globalLexicalBindingEpoch = globalObject->globalLexicalBindingEpoch(); } diff --git a/Source/JavaScriptCore/jit/JITPropertyAccess.cpp b/Source/JavaScriptCore/jit/JITPropertyAccess.cpp index e8e5e0835fb6d..ea3bcce4b21b3 100644 --- a/Source/JavaScriptCore/jit/JITPropertyAccess.cpp +++ b/Source/JavaScriptCore/jit/JITPropertyAccess.cpp @@ -925,6 +925,9 @@ void JIT::emit_op_resolve_scope(const JSInstruction* currentInstruction) // FIXME: This code is weird when caching fails because it goes to a slow path that will check the exact same condition before falling into the C++ slow path. // It's unclear if that makes a meaningful difference for perf but we should consider doing something smarter. switch (profiledResolveType) { +#if USE(BUN_JSC_ADDITIONS) + case InterceptedGlobalProperty: +#endif case GlobalProperty: { addSlowCase(branch32(NotEqual, resolveTypeAddress, TrustedImm32(profiledResolveType))); loadGlobalObject(returnValueGPR); @@ -1066,6 +1069,9 @@ MacroAssemblerCodeRef JIT::generateOpResolveScopeThunk(VM& vm) auto emitCode = [&] (ResolveType resolveType) { switch (resolveType) { +#if USE(BUN_JSC_ADDITIONS) + case InterceptedGlobalProperty: +#endif case GlobalProperty: case GlobalPropertyWithVarInjectionChecks: { // JSScope::constantScopeForCodeBlock() loads codeBlock->globalObject(). @@ -1141,6 +1147,9 @@ MacroAssemblerCodeRef JIT::generateOpResolveScopeThunk(VM& vm) emitCase(GlobalVarWithVarInjectionChecks); emitCase(GlobalPropertyWithVarInjectionChecks); emitCase(GlobalLexicalVarWithVarInjectionChecks); +#if USE(BUN_JSC_ADDITIONS) + emitCase(InterceptedGlobalProperty); +#endif slowCase.append(jit.jump()); skipToEnd.link(&jit); @@ -1242,6 +1251,24 @@ void JIT::emit_op_get_from_scope(const JSInstruction* currentInstruction) loadValue(BaseIndex(scopeGPR, scratch1GPR, TimesEight, (firstOutOfLineOffset - 2) * sizeof(EncodedJSValue)), returnValueJSR); break; } +#if USE(BUN_JSC_ADDITIONS) + case InterceptedGlobalProperty: { + addSlowCase(branch32(NotEqual, scratch1GPR, TrustedImm32(profiledResolveType))); + // The scope must still be this CodeBlock's global (else a global let/const now shadows the name), and the + // cache is against its interceptor's structure. + emitGetVirtualRegister(scope, scopeGPR); + loadGlobalObject(scratch1GPR); + addSlowCase(branchPtr(NotEqual, scopeGPR, scratch1GPR)); + loadPtr(Address(scopeGPR, JSGlobalObject::offsetOfGlobalScopeInterceptor()), scopeGPR); + load32(structureIDAddress, scratch1GPR); + addSlowCase(branch32(NotEqual, Address(scopeGPR, JSCell::structureIDOffset()), scratch1GPR)); + loadPtr(operandAddress, scratch1GPR); + loadProperty(scopeGPR, scratch1GPR, returnValueJSR); + // A property holding the interceptor itself is left to the global's getOwnPropertySlot. + addSlowCase(branch64(Equal, returnValueJSR.payloadGPR(), scopeGPR)); + break; + } +#endif case GlobalVar: { addSlowCase(branch32(NotEqual, scratch1GPR, TrustedImm32(profiledResolveType))); loadPtr(operandAddress, scratch1GPR); @@ -1389,6 +1416,20 @@ MacroAssemblerCodeRef JIT::generateOpGetFromScopeThunk(VM& vm) jit.loadValue(BaseIndex(scopeGPR, scratch1GPR, TimesEight, (firstOutOfLineOffset - 2) * sizeof(EncodedJSValue)), returnValueJSR); break; } +#if USE(BUN_JSC_ADDITIONS) + case InterceptedGlobalProperty: { + loadGlobalObject(jit, scratch1GPR); + slowCase.append(jit.branchPtr(NotEqual, scopeGPR, scratch1GPR)); + jit.loadPtr(Address(scopeGPR, JSGlobalObject::offsetOfGlobalScopeInterceptor()), scopeGPR); + jit.load32(Address(metadataGPR, OpGetFromScope::Metadata::offsetOfStructureID()), scratch1GPR); + slowCase.append(jit.branch32(NotEqual, Address(scopeGPR, JSCell::structureIDOffset()), scratch1GPR)); + jit.loadPtr(Address(metadataGPR, Metadata::offsetOfOperand()), scratch1GPR); + jit.loadProperty(scopeGPR, scratch1GPR, returnValueJSR); + // A property holding the interceptor itself is left to the global's getOwnPropertySlot. + slowCase.append(jit.branch64(Equal, returnValueJSR.payloadGPR(), scopeGPR)); + break; + } +#endif case GlobalVar: case GlobalVarWithVarInjectionChecks: case GlobalLexicalVar: @@ -1452,6 +1493,9 @@ MacroAssemblerCodeRef JIT::generateOpGetFromScopeThunk(VM& vm) emitCase(GlobalVarWithVarInjectionChecks); emitCase(GlobalPropertyWithVarInjectionChecks); emitCase(GlobalLexicalVarWithVarInjectionChecks); +#if USE(BUN_JSC_ADDITIONS) + emitCase(InterceptedGlobalProperty); +#endif slowCase.append(jit.jump()); skipToEnd.link(&jit); @@ -1533,6 +1577,9 @@ void JIT::emit_op_put_to_scope(const JSInstruction* currentInstruction) auto structureIDAddress = metadataAddress.withOffset(Metadata::offsetOfStructureID()); auto operandAddress = metadataAddress.withOffset(Metadata::offsetOfOperand()); auto watchpointSetAddress = metadataAddress.withOffset(Metadata::offsetOfWatchpointSet()); +#if USE(BUN_JSC_ADDITIONS) + auto interceptorOffsetAddress = metadataAddress.withOffset(Metadata::offsetOfInterceptorOffset()); +#endif auto emitCode = [&] (ResolveType resolveType) { switch (resolveType) { @@ -1563,6 +1610,36 @@ void JIT::emit_op_put_to_scope(const JSInstruction* currentInstruction) emitWriteBarrier(scope, value, ShouldFilterValue); break; } +#if USE(BUN_JSC_ADDITIONS) + case InterceptedGlobalProperty: { + constexpr JSValueRegs valueJSR = jsRegT10; + constexpr GPRReg interceptorGPR = regT2; + constexpr GPRReg scratch1GPR1 = regT3; + constexpr GPRReg scratch1GPR2 = regT4; + static_assert(noOverlap(valueJSR, interceptorGPR, scratch1GPR1, scratch1GPR2)); + // The scope must still be this CodeBlock's global (else a global let/const now shadows the name), and the + // cache is against its interceptor's structure. + emitGetVirtualRegister(scope, interceptorGPR); + loadGlobalObject(scratch1GPR2); + addSlowCase(branchPtr(NotEqual, interceptorGPR, scratch1GPR2)); + loadPtr(Address(interceptorGPR, JSGlobalObject::offsetOfGlobalScopeInterceptor()), interceptorGPR); + load32(structureIDAddress, scratch1GPR1); + addSlowCase(branch32(NotEqual, Address(interceptorGPR, JSCell::structureIDOffset()), scratch1GPR1)); + + // Store into the interceptor at the cached offset ... + emitGetVirtualRegister(value, valueJSR); + load32(interceptorOffsetAddress, scratch1GPR1); + storeProperty(valueJSR, interceptorGPR, scratch1GPR1, scratch1GPR2); + // ... and into the global's own variable for the name, if it is one. + loadPtr(operandAddress, scratch1GPR1); + Jump notAVariable = branchTestPtr(Zero, scratch1GPR1); + storeValue(valueJSR, Address(scratch1GPR1)); + notAVariable.link(this); + emitWriteBarrier(interceptorGPR); + emitWriteBarrier(scope, value, ShouldFilterValue); + break; + } +#endif case GlobalVar: case GlobalVarWithVarInjectionChecks: case GlobalLexicalVar: @@ -1657,6 +1734,9 @@ void JIT::emit_op_put_to_scope(const JSInstruction* currentInstruction) emitCase(GlobalVarWithVarInjectionChecks); emitCase(GlobalPropertyWithVarInjectionChecks); emitCase(GlobalLexicalVarWithVarInjectionChecks); +#if USE(BUN_JSC_ADDITIONS) + emitCase(InterceptedGlobalProperty); +#endif addSlowCase(jump()); skipToEnd.link(this); diff --git a/Source/JavaScriptCore/llint/LLIntOfflineAsmConfig.h b/Source/JavaScriptCore/llint/LLIntOfflineAsmConfig.h index 3bf82d66306aa..4f4ea5657c6c7 100644 --- a/Source/JavaScriptCore/llint/LLIntOfflineAsmConfig.h +++ b/Source/JavaScriptCore/llint/LLIntOfflineAsmConfig.h @@ -76,6 +76,12 @@ #define OFFLINE_ASM_BIGINT32 0 #endif +#if USE(BUN_JSC_ADDITIONS) +#define OFFLINE_ASM_BUN_JSC_ADDITIONS 1 +#else +#define OFFLINE_ASM_BUN_JSC_ADDITIONS 0 +#endif + #if USE(LARGE_TYPED_ARRAYS) #define OFFLINE_ASM_LARGE_TYPED_ARRAYS 1 #else diff --git a/Source/JavaScriptCore/llint/LowLevelInterpreter.asm b/Source/JavaScriptCore/llint/LowLevelInterpreter.asm index febdd59471894..bbdec5be74ee9 100644 --- a/Source/JavaScriptCore/llint/LowLevelInterpreter.asm +++ b/Source/JavaScriptCore/llint/LowLevelInterpreter.asm @@ -684,6 +684,9 @@ const GlobalPropertyWithVarInjectionChecks = constexpr GlobalPropertyWithVarInje const GlobalVarWithVarInjectionChecks = constexpr GlobalVarWithVarInjectionChecks const GlobalLexicalVarWithVarInjectionChecks = constexpr GlobalLexicalVarWithVarInjectionChecks const ClosureVarWithVarInjectionChecks = constexpr ClosureVarWithVarInjectionChecks +if BUN_JSC_ADDITIONS + const InterceptedGlobalProperty = constexpr InterceptedGlobalProperty +end const ResolveTypeMask = constexpr GetPutInfo::typeBits const InitializationModeMask = constexpr GetPutInfo::initializationBits diff --git a/Source/JavaScriptCore/llint/LowLevelInterpreter64.asm b/Source/JavaScriptCore/llint/LowLevelInterpreter64.asm index 57c5e29863b40..26b3929768332 100644 --- a/Source/JavaScriptCore/llint/LowLevelInterpreter64.asm +++ b/Source/JavaScriptCore/llint/LowLevelInterpreter64.asm @@ -2869,10 +2869,18 @@ llintOpWithMetadata(op_resolve_scope, OpResolveScope, macro (size, get, dispatch returnConstantScope() .rClosureVarWithVarInjectionChecks: - bineq t0, ClosureVarWithVarInjectionChecks, .rDynamic + bineq t0, ClosureVarWithVarInjectionChecks, .rInterceptedGlobalProperty varInjectionCheck(.rDynamic, t2) resolveScope() +.rInterceptedGlobalProperty: +if BUN_JSC_ADDITIONS + bineq t0, InterceptedGlobalProperty, .rDynamic + getConstantScope(t0) + globalLexicalBindingEpochCheck(.rDynamic, t0, t2) + return(t0) +end + .rDynamic: callSlowPath(_slow_path_resolve_scope) dispatch() @@ -2886,6 +2894,22 @@ macro loadScopeWithStructureCheck(opcodeStruct, get, metadata, scope, scratch, s bineq scratch, %opcodeStruct%::Metadata::m_structureID[metadata], slowPath end +if BUN_JSC_ADDITIONS + # InterceptedGlobalProperty: the scope must (still) be the CodeBlock's global object -- otherwise a global let/const + # now shadows the name and the slow path re-resolves -- and the cache is against its interceptor's structure. + # Leaves the interceptor in `interceptor`. + macro loadInterceptorWithStructureCheck(opcodeStruct, get, metadata, interceptor, scratch, slowPath) + get(m_scope, interceptor) + loadq [cfr, interceptor, 8], interceptor + loadp CodeBlock[cfr], scratch + loadp CodeBlock::m_globalObject[scratch], scratch + bpneq interceptor, scratch, slowPath + loadp JSGlobalObject::m_globalScopeInterceptor[interceptor], interceptor + loadi JSCell::m_structureID[interceptor], scratch + bineq scratch, %opcodeStruct%::Metadata::m_structureID[metadata], slowPath + end +end + llintOpWithMetadata(op_get_from_scope, OpGetFromScope, macro (size, get, dispatch, metadata, return) metadata(t5, t0) @@ -2954,11 +2978,25 @@ llintOpWithMetadata(op_get_from_scope, OpGetFromScope, macro (size, get, dispatc end) .gClosureVarWithVarInjectionChecks: - bineq t0, ClosureVarWithVarInjectionChecks, .gDynamic + bineq t0, ClosureVarWithVarInjectionChecks, .gInterceptedGlobalProperty varInjectionCheck(.gDynamic, t2) loadVariable(get, m_scope, t0) getClosureVar() +.gInterceptedGlobalProperty: +if BUN_JSC_ADDITIONS + bineq t0, InterceptedGlobalProperty, .gDynamic + loadInterceptorWithStructureCheck(OpGetFromScope, get, t5, t0, t1, .gDynamic) + move t0, t3 + loadp OpGetFromScope::Metadata::m_operand[t5], t1 + loadPropertyAtVariableOffset(t1, t0, t2) + # A property holding the interceptor itself is left to the global's getOwnPropertySlot (node:vm substitutes the + # context's globalThis for it). + bqeq t2, t3, .gDynamic + valueProfile(size, OpGetFromScope, m_valueProfile, t2, t5) + return(t2) +end + .gDynamic: callSlowPath(_llint_slow_path_get_from_scope) dispatch() @@ -3078,13 +3116,35 @@ llintOpWithMetadata(op_put_to_scope, OpPutToScope, macro (size, get, dispatch, m dispatch() .pClosureVarWithVarInjectionChecks: - bineq t0, ClosureVarWithVarInjectionChecks, .pModuleVar + bineq t0, ClosureVarWithVarInjectionChecks, .pInterceptedGlobalProperty varInjectionCheck(.pDynamic, t2) loadVariable(get, m_scope, t0) putClosureVar() writeBarrierOnOperands(size, get, m_scope, m_value) dispatch() +.pInterceptedGlobalProperty: +if BUN_JSC_ADDITIONS + bineq t0, InterceptedGlobalProperty, .pModuleVar + loadInterceptorWithStructureCheck(OpPutToScope, get, t5, t0, t1, .pDynamic) + # Store into the interceptor at the cached offset ... + get(m_value, t1) + loadConstantOrVariable(size, t1, t2) + loadi OpPutToScope::Metadata::m_interceptorOffset[t5], t1 + storePropertyAtVariableOffset(t1, t0, t2) + # ... and into the global's own variable for the name, if it is one. + loadp OpPutToScope::Metadata::m_operand[t5], t1 + btpz t1, .pInterceptedGlobalPropertyBarriers + storeq t2, [t1] +.pInterceptedGlobalPropertyBarriers: + loadp CodeBlock[cfr], t3 + loadp CodeBlock::m_globalObject[t3], t3 + loadp JSGlobalObject::m_globalScopeInterceptor[t3], t3 + writeBarrierOnCellAndValueWithReload(t3, t2, macro () end) + writeBarrierOnGlobalObject(size, get, m_value) + dispatch() +end + .pModuleVar: bineq t0, ModuleVar, .pDynamic callSlowPath(_slow_path_throw_strict_mode_readonly_property_write_error) diff --git a/Source/JavaScriptCore/lol/LOLJIT.cpp b/Source/JavaScriptCore/lol/LOLJIT.cpp index 682cabce93f6a..65d5c90d24186 100644 --- a/Source/JavaScriptCore/lol/LOLJIT.cpp +++ b/Source/JavaScriptCore/lol/LOLJIT.cpp @@ -3299,6 +3299,9 @@ MacroAssemblerCodeRef LOLJIT::generateOpGetFromScopeThunk(VM& vm jit.loadPtr(Address(metadataGPR, Metadata::offsetOfOperand()), scratch1GPR); jit.loadValue(BaseIndex(scopeGPR, scratch1GPR, TimesEight, JSLexicalEnvironment::offsetOfVariables()), returnValueJSR); break; +#if USE(BUN_JSC_ADDITIONS) + case InterceptedGlobalProperty: // LOL takes the generic path; the LLInt / Baseline / DFG cache it. +#endif case Dynamic: slowCase.append(jit.jump()); break; @@ -3486,6 +3489,9 @@ void LOLJIT::emit_op_put_to_scope(const JSInstruction* currentInstruction) emitWriteBarrier(m_fastAllocator, allocations, scopeRegs, valueRegs, s_scratch, ShouldFilterValue); break; case ModuleVar: +#if USE(BUN_JSC_ADDITIONS) + case InterceptedGlobalProperty: // LOL takes the generic path; the LLInt / Baseline / DFG cache it. +#endif case Dynamic: addSlowCase(jump()); break; @@ -3818,6 +3824,9 @@ MacroAssemblerCodeRef LOLJIT::generateOpResolveScopeThunk(VM& vm case ClosureVarWithVarInjectionChecks: emitResolveClosure(needsVarInjectionChecks(resolveType)); break; +#if USE(BUN_JSC_ADDITIONS) + case InterceptedGlobalProperty: // LOL takes the generic path; the LLInt / Baseline / DFG cache it. +#endif case Dynamic: slowCase.append(jit.jump()); break; diff --git a/Source/JavaScriptCore/lol/LOLJITOperations.cpp b/Source/JavaScriptCore/lol/LOLJITOperations.cpp index f9a848bd18c56..5e5e8742e8f49 100644 --- a/Source/JavaScriptCore/lol/LOLJITOperations.cpp +++ b/Source/JavaScriptCore/lol/LOLJITOperations.cpp @@ -100,14 +100,22 @@ JSC_DEFINE_JIT_OPERATION(operationResolveScopeForLOL, EncodedJSValue, (CallFrame case GlobalProperty: case GlobalPropertyWithVarInjectionChecks: case UnresolvedProperty: - case UnresolvedPropertyWithVarInjectionChecks: { + case UnresolvedPropertyWithVarInjectionChecks: +#if USE(BUN_JSC_ADDITIONS) + case InterceptedGlobalProperty: +#endif + { if (resolvedScope->isGlobalObject()) { JSGlobalObject* globalObject = uncheckedDowncast(resolvedScope); bool hasProperty = globalObject->hasProperty(globalObject, ident); OPERATION_RETURN_IF_EXCEPTION(scope, encodedJSValue()); if (hasProperty) { ConcurrentJSLocker locker(codeBlock->m_lock); +#if USE(BUN_JSC_ADDITIONS) + metadata.m_resolveType = JSScope::globalPropertyResolveType(globalObject, resolveType); +#else metadata.m_resolveType = needsVarInjectionChecks(resolveType) ? GlobalPropertyWithVarInjectionChecks : GlobalProperty; +#endif metadata.m_globalObject.set(vm, codeBlock, globalObject); metadata.m_globalLexicalBindingEpoch = globalObject->globalLexicalBindingEpoch(); } diff --git a/Source/JavaScriptCore/runtime/CommonSlowPaths.cpp b/Source/JavaScriptCore/runtime/CommonSlowPaths.cpp index 76ada89ab7de0..8e1c3a47b92a1 100644 --- a/Source/JavaScriptCore/runtime/CommonSlowPaths.cpp +++ b/Source/JavaScriptCore/runtime/CommonSlowPaths.cpp @@ -1403,14 +1403,22 @@ JSC_DEFINE_COMMON_SLOW_PATH(slow_path_resolve_scope) case GlobalProperty: case GlobalPropertyWithVarInjectionChecks: case UnresolvedProperty: - case UnresolvedPropertyWithVarInjectionChecks: { + case UnresolvedPropertyWithVarInjectionChecks: +#if USE(BUN_JSC_ADDITIONS) + case InterceptedGlobalProperty: +#endif + { if (resolvedScope->isGlobalObject()) { JSGlobalObject* globalObject = uncheckedDowncast(resolvedScope); bool hasProperty = globalObject->hasProperty(globalObject, ident); CHECK_EXCEPTION(); if (hasProperty) { ConcurrentJSLocker locker(codeBlock->m_lock); +#if USE(BUN_JSC_ADDITIONS) + metadata.m_resolveType = JSScope::globalPropertyResolveType(globalObject, resolveType); +#else metadata.m_resolveType = needsVarInjectionChecks(resolveType) ? GlobalPropertyWithVarInjectionChecks : GlobalProperty; +#endif metadata.m_globalObject.set(vm, codeBlock, globalObject); metadata.m_globalLexicalBindingEpoch = globalObject->globalLexicalBindingEpoch(); } diff --git a/Source/JavaScriptCore/runtime/CommonSlowPathsInlines.h b/Source/JavaScriptCore/runtime/CommonSlowPathsInlines.h index dbb9a3b9917e3..db9a22c45f511 100644 --- a/Source/JavaScriptCore/runtime/CommonSlowPathsInlines.h +++ b/Source/JavaScriptCore/runtime/CommonSlowPathsInlines.h @@ -50,7 +50,11 @@ inline void tryCachePutToScopeGlobal( case UnresolvedProperty: case UnresolvedPropertyWithVarInjectionChecks: { if (scope->isGlobalObject()) { +#if USE(BUN_JSC_ADDITIONS) + ResolveType newResolveType = JSScope::globalPropertyResolveType(uncheckedDowncast(scope), resolveType); +#else ResolveType newResolveType = needsVarInjectionChecks(resolveType) ? GlobalPropertyWithVarInjectionChecks : GlobalProperty; +#endif resolveType = newResolveType; // Allow below caching mechanism to kick in. ConcurrentJSLocker locker(codeBlock->m_lock); metadata.m_getPutInfo = GetPutInfo(metadata.m_getPutInfo.resolveMode(), newResolveType, metadata.m_getPutInfo.initializationMode(), metadata.m_getPutInfo.ecmaMode()); @@ -59,7 +63,11 @@ inline void tryCachePutToScopeGlobal( [[fallthrough]]; } case GlobalProperty: - case GlobalPropertyWithVarInjectionChecks: { + case GlobalPropertyWithVarInjectionChecks: +#if USE(BUN_JSC_ADDITIONS) + case InterceptedGlobalProperty: +#endif + { // Global Lexical Binding Epoch is changed. Update op_get_from_scope from GlobalProperty to GlobalLexicalVar. if (scope->isGlobalLexicalEnvironment()) { JSGlobalLexicalEnvironment* globalLexicalEnvironment = uncheckedDowncast(scope); @@ -78,6 +86,54 @@ inline void tryCachePutToScopeGlobal( return; } +#if USE(BUN_JSC_ADDITIONS) + if (resolveType == InterceptedGlobalProperty) { + VM& vm = getVM(globalObject); + JSGlobalObject* globalObject = codeBlock->globalObject(); + JSObject* interceptor = globalObject->globalScopeInterceptor(); + ASSERT(scope == globalObject && interceptor); + // The global's put() reports (in `slot`) how the interceptor took the store. Cache a replace of an existing + // plain data property of the interceptor itself. If the name is also a variable of the global (a `var` or + // function declared by code in it), the global's put() stores to that too and so does the fast path; if the + // global has it as any other kind of own property (a builtin, say), what its put() does with that can't be + // reproduced, so those stay on the slow path. + if (!slot.isCacheablePut() || slot.type() != PutPropertySlot::ExistingProperty || slot.base() != interceptor || !interceptor->structure()->propertyAccessesAreCacheable()) + return; + uintptr_t variableSlot = 0; + WatchpointSet* variableWatchpointSet = nullptr; + { + SymbolTable* symbolTable = globalObject->symbolTable(); + ConcurrentJSLocker locker(symbolTable->m_lock); + auto iter = symbolTable->find(locker, ident.impl()); + if (iter != symbolTable->end(locker)) { + if (iter->value.isReadOnly()) + return; + variableWatchpointSet = iter->value.watchpointSet(); + variableSlot = reinterpret_cast(globalObject->variableAt(iter->value.scopeOffset()).slot()); + } + } + if (!variableSlot) { + PropertySlot ownSlot(globalObject, PropertySlot::InternalMethodType::VMInquiry, &vm); + bool isOwnPropertyOfGlobal = JSGlobalObject::getOwnPropertySlot(globalObject, globalObject, ident, ownSlot); + ownSlot.disallowVMEntry.reset(); + if (isOwnPropertyOfGlobal) + return; + } + // The fast paths store to the variable without notifying its watchpoint. + if (variableWatchpointSet) + variableWatchpointSet->invalidate(vm, StringFireDetail("InterceptedGlobalProperty put cache")); + Structure* structure = interceptor->structure(); + structure->didCachePropertyReplacement(vm, slot.cachedOffset()); + { + ConcurrentJSLocker locker(codeBlock->m_lock); + metadata.m_structureID.setWithoutWriteBarrier(structure); + metadata.m_interceptorOffset = slot.cachedOffset(); + metadata.m_operand = variableSlot; + } + vm.writeBarrier(codeBlock); + return; + } +#endif if (resolveType == GlobalProperty || resolveType == GlobalPropertyWithVarInjectionChecks) { VM& vm = getVM(globalObject); JSGlobalObject* globalObject = codeBlock->globalObject(); @@ -116,7 +172,11 @@ inline void tryCacheGetFromScopeGlobal( case UnresolvedProperty: case UnresolvedPropertyWithVarInjectionChecks: { if (scope->isGlobalObject()) { +#if USE(BUN_JSC_ADDITIONS) + ResolveType newResolveType = JSScope::globalPropertyResolveType(uncheckedDowncast(scope), resolveType); +#else ResolveType newResolveType = needsVarInjectionChecks(resolveType) ? GlobalPropertyWithVarInjectionChecks : GlobalProperty; +#endif resolveType = newResolveType; // Allow below caching mechanism to kick in. ConcurrentJSLocker locker(codeBlock->m_lock); metadata.m_getPutInfo = GetPutInfo(metadata.m_getPutInfo.resolveMode(), newResolveType, metadata.m_getPutInfo.initializationMode(), metadata.m_getPutInfo.ecmaMode()); @@ -125,7 +185,11 @@ inline void tryCacheGetFromScopeGlobal( [[fallthrough]]; } case GlobalProperty: - case GlobalPropertyWithVarInjectionChecks: { + case GlobalPropertyWithVarInjectionChecks: +#if USE(BUN_JSC_ADDITIONS) + case InterceptedGlobalProperty: +#endif + { // Global Lexical Binding Epoch is changed. Update op_get_from_scope from GlobalProperty to GlobalLexicalVar. if (scope->isGlobalLexicalEnvironment()) { JSGlobalLexicalEnvironment* globalLexicalEnvironment = uncheckedDowncast(scope); @@ -144,6 +208,27 @@ inline void tryCacheGetFromScopeGlobal( return; } +#if USE(BUN_JSC_ADDITIONS) + if (resolveType == InterceptedGlobalProperty) { + JSGlobalObject* globalObject = codeBlock->globalObject(); + JSObject* interceptor = globalObject->globalScopeInterceptor(); + ASSERT(scope == globalObject && interceptor); + // The global's getOwnPropertySlot() reports (in `slot`) where the value came from. Cache a plain data property + // of the interceptor itself. (One currently holding the interceptor isn't worth caching: the fast paths hand + // that value back to getOwnPropertySlot, which may substitute another value for it, so they would always miss.) + if (slot.isCacheableValue() && slot.slotBase() == interceptor && interceptor->structure()->propertyAccessesAreCacheable() && interceptor->getDirect(slot.cachedOffset()) != JSValue(interceptor)) { + Structure* structure = interceptor->structure(); + { + ConcurrentJSLocker locker(codeBlock->m_lock); + metadata.m_structureID.setWithoutWriteBarrier(structure); + metadata.m_operand = slot.cachedOffset(); + } + vm.writeBarrier(codeBlock); + structure->startWatchingPropertyForReplacements(vm, slot.cachedOffset()); + } + return; + } +#endif // Covers implicit globals. Since they don't exist until they first execute, we didn't know how to cache them at compile time. if (resolveType == GlobalProperty || resolveType == GlobalPropertyWithVarInjectionChecks) { ASSERT(scope == globalObject || globalObject->varInjectionWatchpointSet().hasBeenInvalidated()); diff --git a/Source/JavaScriptCore/runtime/GetPutInfo.h b/Source/JavaScriptCore/runtime/GetPutInfo.h index e9cd7111c3462..6dc4bc996e96c 100644 --- a/Source/JavaScriptCore/runtime/GetPutInfo.h +++ b/Source/JavaScriptCore/runtime/GetPutInfo.h @@ -80,7 +80,15 @@ enum ResolveType : unsigned { UnresolvedPropertyWithVarInjectionChecks, // Lexical scope didn't prove anything -- probably because of a 'with' scope. - Dynamic + Dynamic, +#if USE(BUN_JSC_ADDITIONS) + // GlobalProperty on a global object that put another object, its "interceptor", in front of its properties + // (JSGlobalObject::globalScopeInterceptor(); node:vm's contextified globals). Resolves and behaves like + // GlobalProperty, except that get_from_scope / put_to_scope cache against the interceptor's structure and load + // from / store to it (a cached put also stores to the global's own variable slot for the name, if it is one), since that is where + // the global's getOwnPropertySlot / put find the property first. Never combined with var injection checks. + InterceptedGlobalProperty, +#endif }; enum class InitializationMode : unsigned { @@ -114,7 +122,10 @@ ALWAYS_INLINE const char* resolveTypeName(ResolveType type) "ClosureVarWithVarInjectionChecks", "UnresolvedProperty", "UnresolvedPropertyWithVarInjectionChecks", - "Dynamic" + "Dynamic", +#if USE(BUN_JSC_ADDITIONS) + "InterceptedGlobalProperty", +#endif }); return names[type]; } @@ -169,6 +180,10 @@ ALWAYS_INLINE ResolveType makeType(ResolveType type, bool needsVarInjectionCheck case UnresolvedPropertyWithVarInjectionChecks: case Dynamic: return type; +#if USE(BUN_JSC_ADDITIONS) + case InterceptedGlobalProperty: + break; +#endif } RELEASE_ASSERT_NOT_REACHED(); @@ -185,6 +200,9 @@ ALWAYS_INLINE bool needsVarInjectionChecks(ResolveType type) case ResolvedClosureVar: case ModuleVar: case UnresolvedProperty: +#if USE(BUN_JSC_ADDITIONS) + case InterceptedGlobalProperty: +#endif return false; case GlobalPropertyWithVarInjectionChecks: case GlobalVarWithVarInjectionChecks: @@ -199,6 +217,22 @@ ALWAYS_INLINE bool needsVarInjectionChecks(ResolveType type) } } +// GlobalProperty and the types that resolve and are guarded (lexical binding epoch, shadowing by a global let/const) +// exactly like it. +ALWAYS_INLINE bool isGlobalPropertyResolveType(ResolveType type) +{ + switch (type) { + case GlobalProperty: + case GlobalPropertyWithVarInjectionChecks: +#if USE(BUN_JSC_ADDITIONS) + case InterceptedGlobalProperty: +#endif + return true; + default: + return false; + } +} + struct ResolveOp { ResolveOp(ResolveType type, size_t depth, Structure* structure, JSLexicalEnvironment* lexicalEnvironment, WatchpointSet* watchpointSet, uintptr_t operand, UniquedStringImpl* importedName = nullptr) : type(type) diff --git a/Source/JavaScriptCore/runtime/JSGlobalObject.cpp b/Source/JavaScriptCore/runtime/JSGlobalObject.cpp index 06aec3b74d9f1..1f7eb158c8786 100644 --- a/Source/JavaScriptCore/runtime/JSGlobalObject.cpp +++ b/Source/JavaScriptCore/runtime/JSGlobalObject.cpp @@ -2487,13 +2487,28 @@ bool JSGlobalObject::defineOwnProperty(JSObject* object, JSGlobalObject* globalO RELEASE_AND_RETURN(scope, Base::defineOwnProperty(thisObject, globalObject, propertyName, descriptor, shouldThrow)); } +#if USE(BUN_JSC_ADDITIONS) +void JSGlobalObject::setGlobalScopeInterceptor(VM& vm, JSObject* interceptor) +{ + // The global's own structure must not be cacheable, or an access could still be cached against it (bypassing the + // interceptor) by the ordinary GlobalProperty machinery before this is set or by get_by_id on the global. + ASSERT(structure()->typeInfo().prohibitsPropertyCaching()); + ASSERT(interceptor && !m_globalScopeInterceptor); + m_globalScopeInterceptor.set(vm, this, interceptor); +} +#endif + // https://tc39.es/ecma262/#sec-candeclareglobalfunction bool JSGlobalObject::canDeclareGlobalFunction(const Identifier& ident) { auto scope = DECLARE_THROW_SCOPE(vm()); PropertySlot slot(this, PropertySlot::InternalMethodType::GetOwnProperty); +#if USE(BUN_JSC_ADDITIONS) + bool hasProperty = interceptsGlobalScope() ? methodTable()->getOwnPropertySlot(this, this, ident, slot) : getOwnPropertySlot(this, this, ident, slot); +#else bool hasProperty = getOwnPropertySlot(this, this, ident, slot); +#endif RETURN_IF_EXCEPTION(scope, { }); if (!hasProperty) [[likely]] return isStructureExtensible(); @@ -2514,7 +2529,11 @@ void JSGlobalObject::createGlobalFunctionBinding(const Identifier& ident) auto scope = DECLARE_THROW_SCOPE(vm); PropertySlot slot(this, PropertySlot::InternalMethodType::GetOwnProperty); +#if USE(BUN_JSC_ADDITIONS) + bool hasProperty = interceptsGlobalScope() ? methodTable()->getOwnPropertySlot(this, this, ident, slot) : getOwnPropertySlot(this, this, ident, slot); +#else bool hasProperty = getOwnPropertySlot(this, this, ident, slot); +#endif RETURN_IF_EXCEPTION(scope, void()); if (hasProperty) [[unlikely]] { if (slot.attributes() & PropertyAttribute::DontDelete) { @@ -3009,6 +3028,7 @@ void JSGlobalObject::visitChildrenImpl(JSCell* cell, Visitor& visitor) #if USE(BUN_JSC_ADDITIONS) visitor.append(thisObject->m_asyncContextData); + visitor.append(thisObject->m_globalScopeInterceptor); visitor.append(thisObject->m_internalFieldTupleStructure); thisObject->m_ffiFunctionStructure.visit(visitor); thisObject->m_ffiCallbackStructure.visit(visitor); diff --git a/Source/JavaScriptCore/runtime/JSGlobalObject.h b/Source/JavaScriptCore/runtime/JSGlobalObject.h index 2cfbfffa98f72..a40b5805eb286 100644 --- a/Source/JavaScriptCore/runtime/JSGlobalObject.h +++ b/Source/JavaScriptCore/runtime/JSGlobalObject.h @@ -518,6 +518,7 @@ class JSGlobalObject : public JSSegmentedVariableObject { #if USE(BUN_JSC_ADDITIONS) bool m_isAsyncContextTrackingEnabled { false }; + WriteBarrier m_globalScopeInterceptor; WriteBarrier m_asyncContextData; std::unique_ptr m_ffiContext; #endif @@ -773,6 +774,16 @@ class JSGlobalObject : public JSSegmentedVariableObject { #if USE(BUN_JSC_ADDITIONS) bool isAsyncContextTrackingEnabled() const { return m_isAsyncContextTrackingEnabled; } void setAsyncContextTrackingEnabled(bool isEnabled) { m_isAsyncContextTrackingEnabled = isEnabled; } + // A global object whose getOwnPropertySlot / put / defineOwnProperty / deleteProperty overrides put another object, + // the interceptor, in front of its own properties (node:vm's contextified globals: the interceptor is the user's + // sandbox object). Global-scope identifier accesses are then never linked to a symbol-table slot: they resolve as + // InterceptedGlobalProperty, whose slow paths go through those overrides and whose inline caches load from / store to + // the interceptor directly when the property is a plain data property of the interceptor itself; and var / function + // declaration checks and bindings see what the overrides report. Set once, before any code runs in the global. + JSObject* globalScopeInterceptor() const { return m_globalScopeInterceptor.get(); } + bool interceptsGlobalScope() const { return !!m_globalScopeInterceptor; } + JS_EXPORT_PRIVATE void setGlobalScopeInterceptor(VM&, JSObject*); + static constexpr ptrdiff_t offsetOfGlobalScopeInterceptor() { return OBJECT_OFFSETOF(JSGlobalObject, m_globalScopeInterceptor); } static constexpr ptrdiff_t offsetOfAsyncContextData() { return OBJECT_OFFSETOF(JSGlobalObject, m_asyncContextData); } #endif diff --git a/Source/JavaScriptCore/runtime/JSGlobalObjectInlines.h b/Source/JavaScriptCore/runtime/JSGlobalObjectInlines.h index 5efbe29e403bc..669c5e0de152b 100644 --- a/Source/JavaScriptCore/runtime/JSGlobalObjectInlines.h +++ b/Source/JavaScriptCore/runtime/JSGlobalObjectInlines.h @@ -543,6 +543,10 @@ inline bool JSGlobalObject::canDeclareGlobalVar(const Identifier& ident) return true; PropertySlot slot(this, PropertySlot::InternalMethodType::GetOwnProperty); +#if USE(BUN_JSC_ADDITIONS) + if (interceptsGlobalScope()) [[unlikely]] + return methodTable()->getOwnPropertySlot(this, this, ident, slot); +#endif return getOwnPropertySlot(this, this, ident, slot); } @@ -554,8 +558,19 @@ inline void JSGlobalObject::createGlobalVarBinding(const Identifier& ident) auto scope = DECLARE_THROW_SCOPE(vm); PropertySlot slot(this, PropertySlot::InternalMethodType::GetOwnProperty); +#if USE(BUN_JSC_ADDITIONS) + bool hasProperty; + if (interceptsGlobalScope()) [[unlikely]] { + hasProperty = methodTable()->getOwnPropertySlot(this, this, ident, slot); + RETURN_IF_EXCEPTION(scope, void()); + } else { + hasProperty = getOwnPropertySlot(this, this, ident, slot); + scope.assertNoExceptionExceptTermination(); + } +#else bool hasProperty = getOwnPropertySlot(this, this, ident, slot); scope.assertNoExceptionExceptTermination(); +#endif if (hasProperty) [[unlikely]] return; diff --git a/Source/JavaScriptCore/runtime/JSScope.cpp b/Source/JavaScriptCore/runtime/JSScope.cpp index 2963f80fa70ea..4b5ed0a1066b3 100644 --- a/Source/JavaScriptCore/runtime/JSScope.cpp +++ b/Source/JavaScriptCore/runtime/JSScope.cpp @@ -140,6 +140,14 @@ static inline bool abstractAccess(JSGlobalObject* globalObject, JSScope* scope, if (scope->isGlobalObject()) { JSGlobalObject* globalObject = uncheckedDowncast(scope); +#if USE(BUN_JSC_ADDITIONS) + if (globalObject->interceptsGlobalScope()) [[unlikely]] { + // Whether or not the name exists yet, and wherever it lives (interceptor, symbol table, structure), the + // access goes through the global's method table until an inline cache against the interceptor forms. + op = ResolveOp(needsVarInjectionChecks ? GlobalPropertyWithVarInjectionChecks : InterceptedGlobalProperty, 0, nullptr, nullptr, nullptr, 0); + return true; + } +#endif { SymbolTable* symbolTable = globalObject->symbolTable(); ConcurrentJSLocker locker(symbolTable->m_lock); @@ -399,6 +407,15 @@ bool JSScope::isNestedLexicalScope() return uncheckedDowncast(this)->symbolTable()->isNestedLexicalScope(); } +#if USE(BUN_JSC_ADDITIONS) +ResolveType JSScope::globalPropertyResolveType(JSGlobalObject* globalObject, ResolveType current) +{ + if (needsVarInjectionChecks(current)) + return GlobalPropertyWithVarInjectionChecks; + return globalObject->interceptsGlobalScope() ? InterceptedGlobalProperty : GlobalProperty; +} +#endif + JSScope* JSScope::constantScopeForCodeBlock(ResolveType type, CodeBlock* codeBlock) { switch (type) { @@ -406,6 +423,9 @@ JSScope* JSScope::constantScopeForCodeBlock(ResolveType type, CodeBlock* codeBlo case GlobalVar: case GlobalPropertyWithVarInjectionChecks: case GlobalVarWithVarInjectionChecks: +#if USE(BUN_JSC_ADDITIONS) + case InterceptedGlobalProperty: +#endif return codeBlock->globalObject(); case GlobalLexicalVarWithVarInjectionChecks: case GlobalLexicalVar: diff --git a/Source/JavaScriptCore/runtime/JSScope.h b/Source/JavaScriptCore/runtime/JSScope.h index be8fd8d57b6da..424267b7d0332 100644 --- a/Source/JavaScriptCore/runtime/JSScope.h +++ b/Source/JavaScriptCore/runtime/JSScope.h @@ -61,6 +61,10 @@ class JSScope : public JSNonFinalObject { static bool hasConstantScope(ResolveType); static JSScope* NODELETE constantScopeForCodeBlock(ResolveType, CodeBlock*); +#if USE(BUN_JSC_ADDITIONS) + // The type a resolve/get/put that found its name on `globalObject` itself is to be cached as. + static ResolveType globalPropertyResolveType(JSGlobalObject*, ResolveType current); +#endif static void collectClosureVariablesUnderTDZ(JSScope*, TDZEnvironment& result, PrivateNameEnvironment&);