Lisp HUG Maillist Archive

Re: load a fasl file from memory


> It seems like it is, here's what they say about LOAD in the CLHS:
> If filespec is a stream, load determines what kind of stream it is
> and loads directly from the stream.

Thanks!

> So I guess that you can create a stream to a vector containing your
> fasl bytes, and load from that. 
> I am not sure however if it is possible to compile to memory and
> / or if it makes sense, but I'd be curious to know. 

Here I did a small test:

http://paste.lisp.org/display/128108

I can load source file for (foo) from a vector of bytes with load, but no luck with a vector of bytes which contains ofasl code foo (foo); I think it tries to load ofasl as text source.

Best,
 Art


Re: load a fasl file from memory

On 2 Mar 2012, at 19:08, Art Obrezan wrote:
> 
> I can load source file for (foo) from a vector of bytes with load, but no luck with a vector of bytes which contains ofasl code foo (foo); I think it tries to load ofasl as text source.


I'd guess you would in any case need to make the stream a binary stream of some kind, not a character stream.

Re: load a fasl file from memory

> I'd guess you would in any case need to make the stream a
> binary stream of some kind, not a character stream.

I tried a binary stream, but load wanted (read-char) from the stream.

Best,
 Art


Re: load a fasl file from memory

Art Obrezan <artobrezan@yahoo.com> writes:

>> I'd guess you would in any case need to make the stream a
>> binary stream of some kind, not a character stream.
>
> I tried a binary stream, but load wanted (read-char) from the stream.

For what it's worth, other implementations handle the situation
differently. For example, CL:LOAD on a binary input stream in SBCL
results in treating the stream as a FASL.

It doesn't seem to me like the behavior is very precisely specified.

Zach


Re: load a fasl file from memory

On 2 Mar 2012, at 20:16, Art Obrezan wrote:
> 
> I tried a binary stream, but load wanted (read-char) from the stream.

This works (on the mac)

(with-open-file (in "fog.xfasl" :direction :input
                                :element-type '(unsigned-byte 8))
  (load in))

While the equivalent with CHARACTER as element type throws the predictable wobbly.  So I guess it is a matter of working out what the characteristics of the stream are.  I could not synthesize a suitable stream type but I didn't try very hard.

Agree with Zach that it is probably very implementation-dependent.

Re: load a fasl file from memory

As I said in the beginning I want to know if it is possible to load fasl from memory (a vector of bytes), not from a file (your example is loading from a file). But I figured out now that it is possible to load a text source code from a vector of bytes, which is also good.

Best,
 Art

> This works (on the mac)
> 
> (with-open-file (in "fog.xfasl" :direction :input
>                
>                
> :element-type '(unsigned-byte 8))
>   (load in))


Re: load a fasl file from memory

Art Obrezan <artobrezan@yahoo.com> writes:

> As I said in the beginning I want to know if it is possible to load
> fasl from memory (a vector of bytes), not from a file (your example is
> loading from a file). But I figured out now that it is possible to
> load a text source code from a vector of bytes, which is also good.

I think the semantics of *LOAD-PATHNAME* and *LOAD-TRUENAME* mean that
you cannot use CL:LOAD on a stream that is not associated with a file,
so it seems like e.g. a flexi-stream binary input stream would not help,
as I thought it might.

Zach


Re: load a fasl file from memory

On 2 Mar 2012, at 20:48, Art Obrezan wrote:

> As I said in the beginning I want to know if it is possible to load fasl from memory (a vector of bytes), not from a file (your example is loading from a file). But I figured out now that it is possible to load a text source code from a vector of bytes, which is also good.

The thing I was trying to demonstrate is that handing LOAD a suitably-constructed stream (in this case, a stream with elements of (unsigned-byte 8)s constructed from a FASL file) works.  You can easily check that handing it, say, a stream of characters constructed from a FASL file *doesn't* work.

It ought then to be sufficient to work out how to make a stream which LOAD likes, backed by a vector of bytes instead of a file.

In answer to Zach's issue: it probably is enough (in LW) to define a method on PATHNAME for the stream returning something suitably plausible-but-nonsensical (/dev/null?).  That's obviously significantly non-portable.

Re: load a fasl file from memory

Zach Beane <xach@xach.com> writes:

> Art Obrezan <artobrezan@yahoo.com> writes:
>
>> As I said in the beginning I want to know if it is possible to load
>> fasl from memory (a vector of bytes), not from a file (your example is
>> loading from a file). But I figured out now that it is possible to
>> load a text source code from a vector of bytes, which is also good.
>
> I think the semantics of *LOAD-PATHNAME* and *LOAD-TRUENAME* mean that
> you cannot use CL:LOAD on a stream that is not associated with a file,
> so it seems like e.g. a flexi-stream binary input stream would not help,
> as I thought it might.

You'd need to use GRAY-STREAM to define a kind of in-memory file stream,
on which LOAD could call READ, PATHNAME, and FILE-POSITION.

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
A bad day in () is better than a good day in {}.


Updated at: 2020-12-10 08:36 UTC