Skip to content

Commit

Permalink
Correctly pass context parameter to import_value
Browse files Browse the repository at this point in the history
  • Loading branch information
LiraNuna authored and alexjg committed May 16, 2024
1 parent b517bfb commit 7b4bc8a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
10 changes: 6 additions & 4 deletions javascript/src/proxies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ function import_value(
value: any,
textV2: boolean,
path: Prop[],
context: Automerge,
): ImportedValue {
switch (typeof value) {
case "object":
Expand Down Expand Up @@ -255,7 +256,8 @@ const MapHandler = {
if (key === CLEAR_CACHE) {
return true
}
const [value, datatype] = import_value(val, textV2, [...path, key])

const [value, datatype] = import_value(val, textV2, [...path, key], context)
switch (datatype) {
case "list": {
const list = context.putObject(objectId, key, [])
Expand Down Expand Up @@ -374,7 +376,7 @@ const ListHandler = {
if (typeof index == "string") {
throw new RangeError("list index must be a number")
}
const [value, datatype] = import_value(val, textV2, [...path, index])
const [value, datatype] = import_value(val, textV2, [...path, index], context)
switch (datatype) {
case "list": {
let list: ObjID
Expand Down Expand Up @@ -588,7 +590,7 @@ function listMethods<T extends Target>(target: T) {
},

fill(val: ScalarValue, start: number, end: number) {
const [value, datatype] = import_value(val, textV2, [...path, start])
const [value, datatype] = import_value(val, textV2, [...path, start], context)
const length = context.length(objectId)
start = parseListIndex(start || 0)
end = parseListIndex(end || length)
Expand Down Expand Up @@ -680,7 +682,7 @@ function listMethods<T extends Target>(target: T) {
}
const values = vals.map((val, index) => {
try {
return import_value(val, textV2, [...path])
return import_value(val, textV2, [...path], context)
} catch (e) {
if (e instanceof RangeError) {
throw new RangeError(`${e.message} at index ${index} in the input`)
Expand Down
12 changes: 12 additions & 0 deletions javascript/test/proxies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ describe("Proxies", () => {
doc = from({ list: ["a", "b", "c"] })
})

describe("recursive document", () => {
it("should throw a useful RangeError when attempting to set a document inside itself", () => {
type RecursiveDoc = { [key: string]: RecursiveDoc };
const doc = from<RecursiveDoc>({});
change(doc, d => {
assert.throws(() => {
d.doc = doc
}, /Cannot create a reference to an existing document object/)
})
})
})

describe("List Iterators", () => {
it("should return iterable entries", () => {
change(doc, d => {
Expand Down

0 comments on commit 7b4bc8a

Please sign in to comment.