assign method
Implementation
Object? assign(String name, Object? value) {
if (Logger.isDebug) {
Logger.debug(
"[Env.assign] Attempting to assign '$name' = $value in env: $hashCode",
);
}
if (_values.containsKey(name)) {
final existing = _values[name];
// Handle GlobalGetter with setter - call the native setter instead of replacing
if (existing is GlobalGetter) {
if (existing.hasSetter) {
if (Logger.isDebug) {
Logger.debug(
" [Env.assign] Calling setter for GlobalGetter '$name'",
);
}
// Unwrap BridgedEnumValue to its native value before calling the setter.
// GEN-054: This ensures bridged enum values can be assigned to native setters.
final unwrappedValue = _unwrapForSetter(value);
existing.setter!(unwrappedValue);
return value;
} else {
// GlobalGetter without setter - not assignable
throw RuntimeD4rtException(
"Cannot assign to read-only global getter '$name'. "
"This global only has a getter, not a setter.",
);
}
}
if (Logger.isDebug) {
Logger.debug(
" [Env.assign] Assigned '$name' locally in env: $hashCode",
);
}
_values[name] = value;
// S3b additive: keep a slotted local's slot in sync when its value is set
// through `assign` (async-init resume, late init) rather than the original
// `define`, so the debug parity assert holds. O(1) null-guarded — frames
// with no slotted local skip the map probe entirely.
if (_slotIndex != null) {
final slot = _slotIndex![name];
if (slot != null) _slots![slot] = value;
}
return value;
}
if (_bridgedClasses.containsKey(name)) {
throw RuntimeD4rtException(
"Cannot assign to the name of a bridged class: $name",
);
}
// Prevent assigning to bridged enum names
if (_bridgedEnums.containsKey(name)) {
throw RuntimeD4rtException(
"Cannot assign to the name of a bridged enum: $name",
);
}
if (_enclosing != null) {
if (Logger.isDebug) {
Logger.debug(
" [Env.assign] '$name' not found locally, assigning in parent env: ${_enclosing.hashCode}",
);
}
return _enclosing.assign(
name,
value,
); // Delegate to the parent environment
}
Logger.debug(
"[Env.assign] Variable '$name' not found for assignment, throwing error.",
);
throw RuntimeD4rtException("Assigning to undefined variable '$name'.");
}