dlang-community / drepl

A REPL for D
Boost Software License 1.0
78 stars 20 forks source link

Fails on basic operations #79

Closed vsajip closed 4 years ago

vsajip commented 4 years ago

Using this source file foo.d in the current directory:

module foo;

int answer() {
    return 42;
}

class Foo {
    int answer() {
        return 42;
    }
}

I can build it with dmd 2.086.0 without problems on Ubuntu 64-bit (using dmd -offoo.so -fPIC -shared foo.d). However, while importing it with drepl seems to work, I can't do much with it. Trying to call the answer() function:

$ dub run drepl
Building package drepl in /home/vinay/.dub/packages/drepl-0.2.1/drepl/
Performing "debug" build using /usr/bin/dmd for x86_64.
colorize 1.0.5: target for configuration "library" is up to date.
stdx-allocator 2.77.2: target for configuration "library" is up to date.
libdparse 0.8.7: target for configuration "library" is up to date.
drepl 0.2.1: target for configuration "console" is up to date.
To force a rebuild of up-to-date targets, run again with --force.
Running ../../../../.dub/packages/drepl-0.2.1/drepl/drepl 
Welcome to D REPL.
D> import foo;
foo
D> answer()
Program exited with code 127

Trying to instantiate a class:

$ dub run drepl
Building package drepl in /home/vinay/.dub/packages/drepl-0.2.1/drepl/
Performing "debug" build using /usr/bin/dmd for x86_64.
colorize 1.0.5: target for configuration "library" is up to date.
stdx-allocator 2.77.2: target for configuration "library" is up to date.
libdparse 0.8.7: target for configuration "library" is up to date.
drepl 0.2.1: target for configuration "console" is up to date.
To force a rebuild of up-to-date targets, run again with --force.
Running ../../../../.dub/packages/drepl-0.2.1/drepl/drepl 
Welcome to D REPL.
D> import foo;
foo
D> new Foo()
object.Exception@../../.dub/packages/drepl-0.2.1/drepl/src/drepl/engines/dmd.d(218): failed to load /tmp/drepl.0Z5Okp/_mod1.so (/tmp/drepl.0Z5Okp/_mod1.so: undefined symbol: _D3foo3Foo7__ClassZ)
----------------
../../.dub/packages/drepl-0.2.1/drepl/src/drepl/engines/dmd.d:218 void* drepl.engines.dmd.DMDEngine.loadFunc(immutable(char)[], immutable(char)[]) [0x55be00f18203]
../../.dub/packages/drepl-0.2.1/drepl/src/drepl/engines/dmd.d:113 drepl.engines.EngineResult drepl.engines.dmd.DMDEngine.evalExpr(const(char[])) [0x55be00f1758c]
../../.dub/packages/drepl-0.2.1/drepl/src/drepl/interpreter.d:48 drepl.interpreter.InterpreterResult drepl.interpreter.Interpreter!(drepl.engines.dmd.DMDEngine).Interpreter.interpret(const(char)[]) [0x55be00f400d1]
../../.dub/packages/drepl-0.2.1/drepl/src/console.d:29 _Dmain [0x55be00ee74ae]
Program exited with code 1

But the symbol it can't find - _D3foo3Foo7__ClassZ - is definitely in the foo.so that I built using dmd:

$ nm foo.so | grep "foo.*Class"
0000000000277220 V _D3foo3Foo7__ClassZ

Am I using it wrong, or are these known-about problems?

MartinNowak commented 4 years ago

There is no support to link with additional sources or dub packages, see #4. The repl would know how to build (or at least how to link) your project.

As a simple hack, you could ask the runtime linker to preload your shared library.

env LD_PRELOAD=$PWD/foo.so dub run drepl

This does work here, but don't ask me about the implications of preloading a D shared library, expect that it might mess up the C&D runtime initializations.

MartinNowak commented 4 years ago

Duplicate of #4