crystal-lang / crystal

The Crystal Programming Language
https://crystal-lang.org
Apache License 2.0
19.5k stars 1.62k forks source link

Interpreter: Load libraries in REPL mode #12624

Open zw963 opened 2 years ago

zw963 commented 2 years ago
╰─ $ crystal i
icr:1:0> require "yaml"
=> nil
icr:2:0> YAML.parse(File.read("./foo.yml"))
undefined reference to `yaml_event_delete' (Crystal::Loader::LoadError)
  from /home/zw963/Crystal/crystal-lang/crystal/src/compiler/crystal/loader.cr:79:27 in 'find_symbol'
  from /home/zw963/Crystal/crystal-lang/crystal/src/compiler/crystal/interpreter/context.cr:394:5 in 'c_function'
  from /home/zw963/Crystal/crystal-lang/crystal/src/compiler/crystal/interpreter/compiler.cr:1969:17 in 'compile_lib_call'
  from /home/zw963/Crystal/crystal-lang/crystal/src/compiler/crystal/interpreter/compiler.cr:1850:7 in 'visit'
  from /home/zw963/Crystal/crystal-lang/crystal/src/compiler/crystal/syntax/visitor.cr:27:12 in 'accept'
  from /home/zw963/Crystal/crystal-lang/crystal/src/compiler/crystal/interpreter/compiler.cr:592:7 in 'visit'
  from /home/zw963/Crystal/crystal-lang/crystal/src/compiler/crystal/syntax/visitor.cr:27:12 in 'accept'
  from /home/zw963/Crystal/crystal-lang/crystal/src/compiler/crystal/interpreter/compiler.cr:242:5 in 'compile_def'
  from /home/zw963/Crystal/crystal-lang/crystal/src/compiler/crystal/interpreter/compiler.cr:2065:7 in 'create_compiled_def'
  from /home/zw963/Crystal/crystal-lang/crystal/src/compiler/crystal/interpreter/compiler.cr:1863:22 in 'visit'
  from /home/zw963/Crystal/crystal-lang/crystal/src/compiler/crystal/syntax/visitor.cr:27:12 in 'accept'
  from /home/zw963/Crystal/crystal-lang/crystal/src/compiler/crystal/interpreter/compiler.cr:592:7 in 'visit'
  from /home/zw963/Crystal/crystal-lang/crystal/src/compiler/crystal/syntax/visitor.cr:27:12 in 'accept'
  from /home/zw963/Crystal/crystal-lang/crystal/src/compiler/crystal/interpreter/compiler.cr:242:5 in 'compile_def'
  from /home/zw963/Crystal/crystal-lang/crystal/src/compiler/crystal/interpreter/compiler.cr:2065:7 in 'create_compiled_def'
  from /home/zw963/Crystal/crystal-lang/crystal/src/compiler/crystal/interpreter/compiler.cr:1863:22 in 'visit'
  from /home/zw963/Crystal/crystal-lang/crystal/src/compiler/crystal/syntax/visitor.cr:27:12 in 'accept'
  from /home/zw963/Crystal/crystal-lang/crystal/src/compiler/crystal/interpreter/compiler.cr:201:5 in 'compile_block'
  from /home/zw963/Crystal/crystal-lang/crystal/src/compiler/crystal/interpreter/compiler.cr:2144:7 in 'create_compiled_block'
  from /home/zw963/Crystal/crystal-lang/crystal/src/compiler/crystal/interpreter/compiler.cr:1993:7 in 'create_compiled_def'
  from /home/zw963/Crystal/crystal-lang/crystal/src/compiler/crystal/interpreter/compiler.cr:1863:22 in 'visit'
  from /home/zw963/Crystal/crystal-lang/crystal/src/compiler/crystal/syntax/visitor.cr:27:12 in 'accept'
  from /home/zw963/Crystal/crystal-lang/crystal/src/compiler/crystal/interpreter/compiler.cr:242:5 in 'compile_def'
  from /home/zw963/Crystal/crystal-lang/crystal/src/compiler/crystal/interpreter/compiler.cr:2065:7 in 'create_compiled_def'
  from /home/zw963/Crystal/crystal-lang/crystal/src/compiler/crystal/interpreter/compiler.cr:1863:22 in 'visit'
  from /home/zw963/Crystal/crystal-lang/crystal/src/compiler/crystal/syntax/visitor.cr:27:12 in 'accept'
  from /home/zw963/Crystal/crystal-lang/crystal/src/compiler/crystal/interpreter/compiler.cr:242:5 in 'compile_def'
  from /home/zw963/Crystal/crystal-lang/crystal/src/compiler/crystal/interpreter/compiler.cr:2065:7 in 'create_compiled_def'
  from /home/zw963/Crystal/crystal-lang/crystal/src/compiler/crystal/interpreter/compiler.cr:1863:22 in 'visit'
  from /home/zw963/Crystal/crystal-lang/crystal/src/compiler/crystal/syntax/visitor.cr:27:12 in 'accept'
  from /home/zw963/Crystal/crystal-lang/crystal/src/compiler/crystal/interpreter/compiler.cr:158:5 in 'compile'
  from /home/zw963/Crystal/crystal-lang/crystal/src/compiler/crystal/interpreter/interpreter.cr:223:5 in 'interpret'
  from /home/zw963/Crystal/crystal-lang/crystal/src/compiler/crystal/interpreter/repl.cr:102:5 in 'interpret'
  from /home/zw963/Crystal/crystal-lang/crystal/src/compiler/crystal/interpreter/repl.cr:40:17 in 'run'
  from /home/zw963/Crystal/crystal-lang/crystal/src/compiler/crystal/command/repl.cr:37:7 in 'repl'
  from /home/zw963/Crystal/crystal-lang/crystal/src/compiler/crystal/command.cr:100:7 in 'run'
  from /home/zw963/Crystal/crystal-lang/crystal/src/compiler/crystal/command.cr:51:5 in 'run'
  from /home/zw963/Crystal/crystal-lang/crystal/src/compiler/crystal/command.cr:50:3 in 'run'
  from /home/zw963/Crystal/crystal-lang/crystal/src/compiler/crystal.cr:11:1 in '__crystal_main'
  from /home/zw963/Crystal/crystal-lang/crystal/src/crystal/main.cr:115:5 in 'main_user_code'
  from /home/zw963/Crystal/crystal-lang/crystal/src/crystal/main.cr:101:7 in 'main'
  from /home/zw963/Crystal/crystal-lang/crystal/src/crystal/main.cr:127:3 in 'main'
  from /usr/lib/libc.so.6 in '??'
  from /usr/lib/libc.so.6 in '__libc_start_main'
  from ../sysdeps/x86_64/start.S:117 in '_start'
  from ???
icr:3:0> 
straight-shoota commented 2 years ago

The interpreter does not load libraries from the REPL. If you put the same expressions in a file and run that in the interpreter, it works because the library is loaded when the interpreter starts.

I don't think this should be hard to do, but it's just not implemented yet.

asterite commented 2 years ago

It (now) works fine now in #12574

bo-tato commented 1 year ago

I'm using latest main branch built from source and I get the same error for "yaml_event_delete". I can require pure crystal libraries fine in the REPL, it is just libraries that are doing c ffi (big, yaml, ssl, etc) that give errors when loading in REPL