Comments (4)
It's not actually a crash but an uncaught error which causes a fatal error and an abort()
:
*** FATAL ERROR: uncaught: 'cyclic input'
If you wrap the test in a duk_safe_call()
you'll get an ordinary error out of it.
The reason that JSON encode/decode helps is because you'll be creating a new object so it removes the cyclic result. To illustrate, before the JSON encode/decode the structure will look like this:
{ x: 'x' } -> { x: 'x', savecopy: [x] }
^ |
`---------------------------------'
which contains a cycle and will serialize to an infinite JSON output.
After the JSON encode/decode it will look like this:
{ x: 'x' } -> { x: 'x', savecopy: [x] }
|
`----> { x: 'x' }
which is cycle free.
The issue here is that duk_push_context_dump() doesn't deal with cyclic structures at present. Ideally it would replace the cycle with a placeholder or such.
from duktape.
Many thanks for the very clear explanation, it does make sense to me.
I am trying to get the info copied into 'savecopy' - so I guess the function I am looking for is duk_copy_top() - i.e. I definitely don't want the infinitely linked scenario as you have described in your post, as I want to be able to change the first value of 'x' but not the nested value.
Is there a more elegant way to achieve what I am looking for than duk_dup_top(), duk_json_encode(), duk_json_decode()?
Again, thanks!
Steve
from duktape.
In general making a deep copy of an arbitrary object is non-trivial for several reasons (loops or shared objects in a DAG arrangement, native properties like file handles, etc), and different use cases may require slightly different approaches. There's currently no direct API to do that, so you'd have to implement a deep copy yourself in some way.
The closest you can probably get now, out of the box, is duk_cbor_encode()
+ duk_cbor_decode()
. This is faster than a JSON encode/decode round trip, and supports more data types than JSON serialization which might matter in some cases.
If the input is always an object decoded from JSON, this way of cloning should be equivalent to JSON cloning, but just a bit faster.
Finally, if the input is always a JSON string, you could also just decode it twice to get two independent copies of the same logical object which saves the JSON encode operation in the cloning process.
Hope this helps :)
from duktape.
Many thanks for your clear and comprehensive responses.
S.
from duktape.
Related Issues (20)
- stack-overflow in duktape/duk_js_call.c:1361 in duk__call_setup_act_attempt_tailcall
- stack-overflow in duktape/duk_api_stack.c:337 in duk_is_valid_posidx
- stack-overflow in duk__try_push_vsprintf in duktape/duk_api_stack.c:4800:8
- stack-overflow in duktape/duk_hobject_assert.c:215 in duk_hthread_assert_valid
- stack-overflow in duktape/duk_js_call.c:1570 in duk__call_setup_act_not_tailcall
- stack-overflow in duktape/duk_hobject_props.c:272 in duk__hobject_get_entry_object_stridx
- Security concern
- Help wanted with nested objects
- Iterating keys of proxy object HOT 1
- make file which also installs duktape fails HOT 1
- Determining when code finished execution when using duk_suspend
- Q: Bytecode only compilation flag
- Support App Privacy Manifest
- duk_create_heap_default not found HOT 1
- SyntaxError: unterminated statement when trying to define a regex HOT 1
- duk_put_number_list constant?
- heap-buffer-overflow duk_heap_stringcache.c:250:7 in duk__strcache_scan_char2byte_wtf8_backwards_2
- stack-overflow duk_js_executor.c:3084 in duk__js_execute_bytecode_inner
- Calling the "toString()" method for a function does not return its source code HOT 1
- [Question] Duktape coroutine and builtins
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from duktape.