Comments (16)
This is really awesome!!! This would be awesome for scientific computing, machine learning, data science, and a whole bunch of other things in Janet!
As for marshaling, we probably need an extension to the C API for abstract types to allow for custom marshalling and unmarshalling code, like how get
and put
were added for abstract types.
from janet.
Another question is "should we make this part of the core library?" I'm leaning towards yes for now, but I think that would depend on large this gets. If it is included, we should probably include an option to disable it at compile time like the peg
module and the asm
functions. Making it a native module instead I think would also be OK.
from janet.
I think making this a "core" feature of Janet could have numerous advantages.
I've already included a JANET_NO_TYPED_ARRAY
flag to disable it at compile time.
My intention was to implementent only the bare minimal code in the corelib
(not much that's already there + marshal code ).
Letting all other "potentially cool features" as extern extensions.
I completely agree with you that janet core should remain as light as possible.
from janet.
Some feedback about the global design of the implementation are welcome.
from janet.
This looks fine to me in general. Are you planning to add some other operators/methods for typed arrays? A slice method would be very useful. Open a pull request when you think its ready.
from janet.
Work in progress with typed arrays
Marshal/unmarshal is implemented.
janet:34:> (def a (tarray/new :float64 10))
<ta/float64 0x000005CA1750>
janet:67:> (def b (unmarshal (marshal a))
janet:98:(> )
<ta/float64 0x000005CA3B40>
janet:100:> (tarray/properties b)
{ :buffer <ta/buffer 0x000005CA3BD0>
:size 10
:stride 1
:type :float64
:byte-offset 0
:type-size 8}
janet:122:> (tarray/properties a)
{ :buffer <ta/buffer 0x000005CA17E0>
:size 10
:stride 1
:type :float64
:byte-offset 0
:type-size 8}
At this point , I need some feedback on the implentation choices.
For this the first step was to give "generic" marshalling capabilities
to abstract type (AT).
This require some "introspection capabilities" to AT.
janet:0:> (abstract/properties :ta/float64)
{ :size 40
:tag 1.69717e+09
:name :ta/float64
:salt 1010
:marshal true}
I do this by adding the possibility to register AT in the vm.
typedef struct {
const JanetAbstractType *at;
size_t size;
const uint32_t salt;
uint32_t tag;
void (* marshal)(void *p,JanetMarshalContext *ctx);
void (* unmarshal)(void *p,JanetMarshalContext *ctx);
} JanetAbstractTypeInfo;
JANET_API void janet_register_abstract_type(const JanetAbstractTypeInfo * info);
This allow to extend AT whitout breaking light "standard" janet AT.
I use the janet_vm_table to register these AT.
It seems to work, but i'm not completely sure about this choice.
If you can take a look at the code, it will help.
https://github.com/jfcap/janet.git (branch typed-array)
from janet.
Lots of progress!
I've taken a look at all of the changes and a couple of things I've noticed about the design:
This allow to extend AT whithout breaking light "standard" janet AT.
Wouldn't it be easier to just extend JanetAbstractType to have some of the extra fields in JanetAbstractTypeInfo, and drop JanetAbstractTypeInfo altogether? I think simplicity should be prioritized over backwards compatibility at this point - we are still pre 1.0. We could just add the marshal and unmarshal elements to JanetAstractType, and existing abstract type definitions would just need two extra NULL pointers in the initializers - not that much work, and a pretty clear error message if someone tries to compile an old native module.
As for the interface:
janet_register_abstract_type(const JanetAbstractTypeInfo *info);
becomes
janet_register_abstract_type(const JanetAbstractType *atype)
The tag/salt/name system seems too complicated, and only needed because types and type_info are separate. Two alternatives:
- Require the programmer to manually set tag to a unique 32 bit integer for each type. No hashing, no salt, simple. Similar to what Java does for it's
Serializable
interface andserialVersionUID
. The overhead could be 4 bytes per abstract. - Prefix marshalled abstracts by the name of their type. Like
"ta/buffer"|length|data
. Multiple abstracts of the same type would be efficient, since marshal caches strings, subsequent references to "ta/buffer" will be pretty short, often only 2 or 3 bytes. I think this will be the easiest, and requires types to have only one unique identifier, their name.
These changes would simplify both the interface and the code, although I may be overlooking something.
from janet.
Here a stripped down version with AT name as identifier for marshaling.
https://github.com/jfcap/janet.git (branch typed-array)
To be done :
- tarray --> @[] array slice method
- tarray/buffer --> @"" buffer method ?
- a test suite
what else ?
from janet.
Looks pretty good so far. Just looking at it, in the unmarshalling functions (janet_unmarshal_uint
and so on) should check for the end of source with the MARSH_EOS macro. Unmarshalling should be resistant to bad input, although I'm not sure how safe the current unmarshal function is on bad input (safe in theory, not adequately tested in practice).
from janet.
Unmarshalling should be resistant to bad input,
Is it really possible "to protect janet" from bad (forged) marshalled data ?
from janet.
It’s more that Janet shouldn’t segfault on bad data or construct functions that will cause segfaults later, it should throw an error.
from janet.
There’s nothing wronged with “forged” data, as long as the data follows the protocol. Data that doesn’t follow the protocol should not be able to crash Janet.
from janet.
What bothers me, is for example in the unmarshalling code for tarray views :
static void ta_view_unmarshal(void *p, JanetMarshalContext *ctx) {
JanetTArrayView *view = (JanetTArrayView *)p;
size_t offset;
Janet buffer;
janet_unmarshal_size(ctx, &(view->size));
janet_unmarshal_size(ctx, &(view->stride));
janet_unmarshal_uint(ctx, &(view->type));
janet_unmarshal_size(ctx, &offset);
janet_unmarshal_janet(ctx, &buffer);
view->buffer = (JanetTArrayBuffer *)janet_unwrap_abstract(buffer);
view->data = view->buffer->data + offset;
}
to be "forged marshalled data safe" I've to put some guards around view->data = view->buffer->data + offset;
. It's not a big deal here, and I will do it.
Nevertheless I must admit that i'm not verry confortable with the idea that "marshalled data" could be assumed safe for the vm.
Peraphs i'm missing something here, but is marshalling/unmarshalling vm byte code not always inherently unsafe ?
from janet.
Nope, Janet can verify all bytecode.
from janet.
By that I mean Janet bytecode shouldn’t cause a segfault - of course, malicious bytecode could do other more terrible things.
from janet.
Ok, This is what I meant.
from janet.
Related Issues (20)
- eventloop/networking gets stuck on musl HOT 11
- Consider making `module/paths` a dynamic variable HOT 7
- Can it run on iOS? Thank you. HOT 3
- `os/strftime` doesn't respect environment variables. HOT 3
- Re-integrate jpm into janet HOT 49
- recent change kinda breaks lexical scope with multiple modules HOT 2
- How to switch off spork/sh/copy message? HOT 2
- `janet -c source` with missing output files should give informative error message HOT 1
- Linux event loop hanging HOT 7
- Certain PEG patterns don't match HOT 2
- Discrepancy between the website docs and behavior for the `look` peg special HOT 4
- `release-fix` Branch is 5 Commits Behind? HOT 2
- os/open fails to create a file unless it's opened read/write-only HOT 1
- Warning/error on seq with no body HOT 5
- Undefined behaviour when running `make test` with `-fsanitize=undefined` HOT 5
- short-fn and -> don't work together HOT 6
- Add an option to os/pipe to create synchronous pipe HOT 5
- Possible retagging of v1.35.2 HOT 11
- Awkwardness of `dest` argument for `bundle/add-bin`? HOT 3
- Changing the files used by the `bundle/` functions HOT 4
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 janet.