Skip to content

Commit

Permalink
make script vars unlink aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
dragazo committed Nov 30, 2023
1 parent 30112a0 commit 5d65b89
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 12 deletions.
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#![forbid(unsafe_code)]
#![no_std]

#![recursion_limit = "256"]

#![doc = include_str!("../README.md")]

#[cfg(feature = "std")]
Expand Down
2 changes: 1 addition & 1 deletion src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,7 @@ impl<'gc, C: CustomTypes<S>, S: System<C>> Process<'gc, C, S> {
}

Instruction::DeclareLocal { var } => {
self.call_stack.last_mut().unwrap().locals.define_if_undefined(var, || Shared::Unique(Number::new(0.0).unwrap().into()));
self.call_stack.last_mut().unwrap().locals.define_or_redefine(var, Shared::Unique(Number::new(0.0).unwrap().into()));
self.pos = aft_pos;
}
Instruction::InitUpvar { var } => {
Expand Down
7 changes: 0 additions & 7 deletions src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1206,13 +1206,6 @@ impl<'gc, C: CustomTypes<S>, S: System<C>> SymbolTable<'gc, C, S> {
pub fn define_or_redefine(&mut self, var: &str, value: Shared<'gc, Value<'gc, C, S>>) {
self.0.insert(var.to_owned(), value);
}
/// Defines a variable with an initial value if it does not already exist in this symbol table.
/// If a variable with the given name already exists, this is a no-op.
pub fn define_if_undefined<F: FnOnce() -> Shared<'gc, Value<'gc, C, S>>>(&mut self, var: &str, f: F) {
if !self.0.contains_key(var) {
self.0.insert(var.to_owned(), f());
}
}
/// Looks up the given variable in the symbol table.
/// If a variable with the given name does not exist, returns [`None`].
pub fn lookup(&self, var: &str) -> Option<&Shared<'gc, Value<'gc, C, S>>> {
Expand Down
2 changes: 1 addition & 1 deletion src/test/blocks/upvars.xml

Large diffs are not rendered by default.

42 changes: 39 additions & 3 deletions src/test/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ fn test_proc_lambda_local_shadow_capture() {
), Settings::default(), system, |_| SymbolTable::default());

run_till_term(&mut env, |mc, _, res| {
let expect = Value::from_simple(mc, SimpleValue::from_json(json!(["1", "1", "1"])).unwrap());
let expect = Value::from_simple(mc, SimpleValue::from_json(json!(["1", 0, "1"])).unwrap());
assert_values_eq(&res.unwrap().0, &expect, 1e-20, "local shadow capture");
});
}
Expand All @@ -374,6 +374,17 @@ fn test_proc_upvars() {

run_till_term(&mut env, |mc, _, res| {
let expect = Value::from_simple(mc, SimpleValue::from_json(json!([
[0.6427876096865393,0],
[0.984807753012208,0],
[0.8660254037844387,0],
[0.3420201433256689,0],
[-0.34202014332566866,0],
[-0.8660254037844384,0],
[-0.9848077530122081,0],
[-0.6427876096865396,0],
"---",
[-0.6427876096865396,0],
"---",
[0.6427876096865393,0.766044443118978],
[0.984807753012208,0.17364817766693041],
[0.8660254037844387,-0.4999999999999998],
Expand All @@ -385,6 +396,17 @@ fn test_proc_upvars() {
"---",
[-0.6427876096865396,0.7660444431189778],
"---",
[0.6427876096865393,0],
[0.984807753012208,0],
[0.8660254037844387,0],
[0.3420201433256689,0],
[-0.34202014332566866,0],
[-0.8660254037844384,0],
[-0.9848077530122081,0],
[-0.6427876096865396,0],
"---",
[-0.6427876096865396,0],
"---",
[0.6427876096865393,0.766044443118978],
[0.984807753012208,0.17364817766693041],
[0.8660254037844387,-0.4999999999999998],
Expand All @@ -397,7 +419,14 @@ fn test_proc_upvars() {
[-0.6427876096865396,0.7660444431189778],
"---",
"---",
["dvdf","htyhr"],
[0,0],
"---",
[0.3420201433256687,0],
[0.6427876096865393,0],
[0.8660254037844386,0],
[0.984807753012208,0],
"---",
[0.984807753012208,0],
"---",
[0.3420201433256687,0.9396926207859084],
[0.6427876096865393,0.766044443118978],
Expand All @@ -406,6 +435,13 @@ fn test_proc_upvars() {
"---",
[0.984807753012208,0.17364817766693041],
"---",
[0.3420201433256687,0],
[0.6427876096865393,0],
[0.8660254037844386,0],
[0.984807753012208,0],
"---",
[0.984807753012208,0],
"---",
[0.3420201433256687,0.9396926207859084],
[0.6427876096865393,0.766044443118978],
[0.8660254037844386,0.5000000000000001],
Expand All @@ -414,7 +450,7 @@ fn test_proc_upvars() {
[0.984807753012208,0.17364817766693041],
"---",
"---",
["gfdgr","rjhrthr"],
[0,0],
"---",
])).unwrap());
assert_values_eq(&res.unwrap().0, &expect, 1e-10, "upvars");
Expand Down
51 changes: 51 additions & 0 deletions src/test/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,57 @@ fn test_proj_broadcast() {
});
}

#[test]
fn test_proj_delayed_capture_upvar() {
let system = Rc::new(StdSystem::new_sync(BASE_URL.to_owned(), None, Config::default(), Arc::new(Clock::new(UtcOffset::UTC, None))));
let proj = get_running_project(include_str!("projects/delayed-capture-upvar.xml"), system);
proj.mutate(|mc, proj| {
run_till_term(mc, &mut *proj.proj.borrow_mut(mc)).unwrap();
let global_context = proj.proj.borrow().get_global_context();
let global_context = global_context.borrow();

let expected = Value::from_simple(mc, SimpleValue::from_json(json!([
["test 1", 13],
["test 1", 13],
["test 1", 13],
["test 2", 13],
["test 2", 13],
["test 2", 13],
["test 3", 13],
["test 3", 13],
["test 3", 13],
["tracking 4", 0],
["tracking 4", 0],
["tracking 4", 0],
["test 4", 11],
["test 4", 12],
["test 4", 13],
["tracking 5", 0],
["tracking 5", 0],
["tracking 5", 0],
["test 5", 11],
["test 5", 12],
["test 5", 13],
["tracking 6", 0],
["tracking 6", 0],
["tracking 6", 0],
["test 6", 11],
["test 6", 12],
["test 6", 13],
["test 7", 11],
["test 7", 12],
["test 7", 13],
["test 8", 11],
["test 8", 12],
["test 8", 13],
["test 9", 11],
["test 9", 12],
["test 9", 13],
])).unwrap());
assert_values_eq(&global_context.globals.lookup("res").unwrap().get().clone(), &expected, 1e-20, "res");
});
}

#[test]
fn test_proj_broadcast_to() {
let system = Rc::new(StdSystem::new_sync(BASE_URL.to_owned(), None, Config::default(), Arc::new(Clock::new(UtcOffset::UTC, None))));
Expand Down
1 change: 1 addition & 0 deletions src/test/projects/delayed-capture-upvar.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<room name="untitled" app="NetsBlox 2.2.0, http://netsblox.org"><role name="myRole"><project collabStartIndex="181" name="myRole" app="NetsBlox 2.2.0, http://netsblox.org" version="2.2.0"><notes></notes><stage name="Stage" width="480" height="360" collabId="item_-1_1" costume="0" color="255,255,255,1" tempo="60" threadsafe="false" penlog="false" volume="100" pan="0" lines="round" ternary="false" hyperops="true" codify="false" inheritance="false" sublistIDs="false" scheduled="false" id="1"><costumes><list struct="atomic" id="2"></list></costumes><sounds><list struct="atomic" id="3"></list></sounds><variables></variables><blocks></blocks><messageTypes><messageType><name>message</name><fields><field>msg</field></fields></messageType></messageTypes><scripts></scripts><sprites><sprite name="Sprite" collabId="item_-1" idx="1" x="0" y="0" heading="90" scale="1" volume="100" pan="0" rotation="1" draggable="true" costume="0" color="80,80,80,1" pen="tip" id="10"><costumes><list struct="atomic" id="11"></list></costumes><sounds><list struct="atomic" id="12"></list></sounds><blocks></blocks><variables></variables><scripts><script x="14.285714285714286" y="7.142857142857143"><block collabId="item_87" s="receiveGo"></block><block collabId="item_80_2" s="doWarp"><script><block collabId="item_80_5" s="doFor"><l>i</l><l>11</l><l>13</l><script><block collabId="item_80_6" s="fork"><block collabId="item_80_10" s="reifyScript"><script><block collabId="item_80_11" s="doAddToList"><block collabId="item_80_18" s="reportNewList"><list><l>test 1</l><block collabId="item_80_20" var="i"/></list></block><block collabId="item_80_19" var="res"/></block></script><list></list></block><list></list></block></script></block></script></block><block collabId="item_80_4" s="doWait"><l>0</l></block><block collabId="item_80_7" s="doWarp"><script><block collabId="item_80_9" s="doForEach"><l>item</l><block collabId="item_80_12" s="reportNumbers"><l>11</l><l>13</l></block><script><block collabId="item_80_13" s="fork"><block collabId="item_80_17" s="reifyScript"><script><block collabId="item_80_21" s="doAddToList"><block collabId="item_80_27" s="reportNewList"><list><l>test 2</l><block collabId="item_80_29" var="item"/></list></block><block collabId="item_80_28" var="res"/></block></script><list></list></block><list></list></block></script></block></script></block><block collabId="item_80_8" s="doWait"><l>0</l></block><block collabId="item_80_14" s="doWarp"><script><custom-block collabId="item_80_16" s="my for each %upvar in %l %cs"><l>thingy</l><block collabId="item_80_22" s="reportNumbers"><l>11</l><l>13</l></block><script><block collabId="item_80_23" s="fork"><block collabId="item_80_26" s="reifyScript"><script><block collabId="item_80_30" s="doAddToList"><block collabId="item_80_31" s="reportNewList"><list><l>test 3</l><block collabId="item_80_33" var="thingy"/></list></block><block collabId="item_80_32" var="res"/></block></script><list></list></block><list></list></block></script></custom-block></script></block><block collabId="item_100" s="doWait"><l>0</l></block><block collabId="item_117" s="doWarp"><script><block collabId="item_117_2" s="doFor"><l>i</l><l>11</l><l>13</l><script><block collabId="item_121" s="doDeclareVariables"><list><l>a</l></list></block><block collabId="item_144" s="doAddToList"><block collabId="item_144_1" s="reportNewList"><list><l>tracking 4</l><block collabId="item_144_3" var="a"/></list></block><block collabId="item_144_2" var="res"/></block><block collabId="item_124" s="doSetVar"><l>a</l><block collabId="item_126" var="i"/></block><block collabId="item_117_3" s="fork"><block collabId="item_117_7" s="reifyScript"><script><block collabId="item_117_8" s="doAddToList"><block collabId="item_117_15" s="reportNewList"><list><l>test 4</l><block collabId="item_123" var="a"/></list></block><block collabId="item_117_16" var="res"/></block></script><list></list></block><list></list></block></script></block></script></block><block collabId="item_117_1" s="doWait"><l>0</l></block><block collabId="item_117_4" s="doWarp"><script><block collabId="item_117_6" s="doForEach"><l>item</l><block collabId="item_117_9" s="reportNumbers"><l>11</l><l>13</l></block><script><block collabId="item_127" s="doDeclareVariables"><list><l>a</l></list></block><block collabId="item_146" s="doAddToList"><block collabId="item_146_1" s="reportNewList"><list><l>tracking 5</l><block collabId="item_146_3" var="a"/></list></block><block collabId="item_146_2" var="res"/></block><block collabId="item_128" s="doSetVar"><l>a</l><block collabId="item_130" var="item"/></block><block collabId="item_117_10" s="fork"><block collabId="item_117_14" s="reifyScript"><script><block collabId="item_117_18" s="doAddToList"><block collabId="item_117_22" s="reportNewList"><list><l>test 5</l><block collabId="item_132" var="a"/></list></block><block collabId="item_117_23" var="res"/></block></script><list></list></block><list></list></block></script></block></script></block><block collabId="item_117_5" s="doWait"><l>0</l></block><block collabId="item_117_11" s="doWarp"><script><custom-block collabId="item_117_13" s="my for each %upvar in %l %cs"><l>thingy</l><block collabId="item_117_19" s="reportNumbers"><l>11</l><l>13</l></block><script><block collabId="item_133" s="doDeclareVariables"><list><l>a</l></list></block><block collabId="item_148" s="doAddToList"><block collabId="item_148_1" s="reportNewList"><list><l>tracking 6</l><block collabId="item_148_3" var="a"/></list></block><block collabId="item_148_2" var="res"/></block><block collabId="item_134" s="doSetVar"><l>a</l><block collabId="item_136" var="thingy"/></block><block collabId="item_117_20" s="fork"><block collabId="item_117_21" s="reifyScript"><script><block collabId="item_117_25" s="doAddToList"><block collabId="item_117_26" s="reportNewList"><list><l>test 6</l><block collabId="item_138" var="a"/></list></block><block collabId="item_117_27" var="res"/></block></script><list></list></block><list></list></block></script></custom-block></script></block><block collabId="item_117_12" s="doWait"><l>0</l></block><block collabId="item_101" s="doWarp"><script><block collabId="item_101_2" s="doFor"><l>i</l><l>11</l><l>13</l><script><block collabId="item_101_3" s="fork"><block collabId="item_101_7" s="reifyScript"><script><block collabId="item_101_8" s="doAddToList"><block collabId="item_101_15" s="reportNewList"><list><l>test 7</l><block collabId="item_108" var="#1"/></list></block><block collabId="item_101_16" var="res"/></block></script><list><l>#1</l></list></block><list><block collabId="item_112" var="i"/></list></block></script></block></script></block><block collabId="item_101_1" s="doWait"><l>0</l></block><block collabId="item_101_4" s="doWarp"><script><block collabId="item_101_6" s="doForEach"><l>item</l><block collabId="item_101_9" s="reportNumbers"><l>11</l><l>13</l></block><script><block collabId="item_101_10" s="fork"><block collabId="item_101_14" s="reifyScript"><script><block collabId="item_101_18" s="doAddToList"><block collabId="item_101_22" s="reportNewList"><list><l>test 8</l><block collabId="item_109" var="#1"/></list></block><block collabId="item_101_23" var="res"/></block></script><list><l>#1</l></list></block><list><block collabId="item_113" var="item"/></list></block></script></block></script></block><block collabId="item_101_5" s="doWait"><l>0</l></block><block collabId="item_101_11" s="doWarp"><script><custom-block collabId="item_101_13" s="my for each %upvar in %l %cs"><l>thingy</l><block collabId="item_101_19" s="reportNumbers"><l>11</l><l>13</l></block><script><block collabId="item_101_20" s="fork"><block collabId="item_101_21" s="reifyScript"><script><block collabId="item_101_25" s="doAddToList"><block collabId="item_101_26" s="reportNewList"><list><l>test 9</l><block collabId="item_110" var="#1"/></list></block><block collabId="item_101_27" var="res"/></block></script><list><l>#1</l></list></block><list><block collabId="item_114" var="thingy"/></list></block></script></custom-block></script></block><block collabId="item_101_12" s="doWait"><l>0</l></block></script><script x="216.42857142857144" y="16.14285714285716"><block collabId="item_80_1" s="doSetVar"><l>res</l><block collabId="item_80_3" s="reportNewList"><list></list></block></block></script></scripts><history></history></sprite><watcher var="res" style="normal" x="10" y="10" color="243,118,29" extX="112" extY="98.00000000000001"/></sprites></stage><hidden></hidden><headers></headers><code></code><blocks><block-definition collabId="item_-1_2" s="my for each %&apos;item&apos; in %&apos;arr&apos; %&apos;f&apos;" type="command" category="custom"><header></header><code></code><translations></translations><inputs><input type="%upvar"></input><input type="%l"></input><input type="%cs"></input></inputs><script><block collabId="item_4" s="doIf"><block collabId="item_5" s="reportNot"><block collabId="item_6" s="reportListIsEmpty"><block collabId="item_7" var="arr"/></block></block><script><block collabId="item_8" s="doFor"><l>i</l><l>1</l><block collabId="item_9" s="reportListAttribute"><l><option>length</option></l><block collabId="item_10" var="arr"/></block><script><block collabId="item_14" s="doSetVar"><l>item</l><block collabId="item_16" s="reportListItem"><block collabId="item_17" var="i"/><block collabId="item_18" var="arr"/></block></block><block collabId="item_12" s="doRun"><block collabId="item_13" var="f"/><list></list></block></script></block></script></block></script></block-definition></blocks><variables><variable name="res"><list struct="atomic" id="370"></list></variable></variables><history></history><replay></replay></project><media name="myRole" app="NetsBlox 2.2.0, http://netsblox.org" version="2.2.0"></media></role></room>

0 comments on commit 5d65b89

Please sign in to comment.