rb_import 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cae0ed3b02e393c26df865553f46dbfdddd6640b
4
- data.tar.gz: 493283748bf250d7464bc4f85a098d45e16217e0
3
+ metadata.gz: f1e9c432a8f32cac0839e42bdf6055415b2aea1d
4
+ data.tar.gz: ed2a36e286c606b83e061c524f76b31331dcb5f4
5
5
  SHA512:
6
- metadata.gz: e1cbb347253ea12991453dd64f0b61526ff1b3e7c143214b5949639a5688cb9de60b5617376bd394f157ab2be6084e3a910a340472a6b96134c5cbe7483ac570
7
- data.tar.gz: 6b8130e2ac1636a1c1c92b4f3c7fcd2e5db8cab65c302eb998c1f594861e3678c94c5a9ee89e2d11b368a5a6c0880e675a78fe3d2fd7e53fe68412b674a8cac6
6
+ metadata.gz: 442c1ba01a463cdddcf2b99a2702409cc7a3800e2f1880f08b4b84eec57b5bbb5938a2bfddb977782a42d2aa1e0815427cc7423c9e732286ed1bc8486e2aa48f
7
+ data.tar.gz: e71d49660215101c0cb031ad40e61b25bf578ebe252536256c6643d0a82c5ed1741c9ddfc4d03604469e3434094ecf624917c28da5fbf75c61c2f10ffc21b81c
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -1,3 +1,11 @@
1
- # Unreleased (0.1.0)
1
+ # Unreleased
2
+
3
+ N/A
4
+
5
+ # 0.2.0
6
+
7
+ * Code cleansing
8
+
9
+ # 0.1.0
2
10
 
3
11
  * Initial release
data/Rakefile CHANGED
@@ -26,8 +26,6 @@ end
26
26
  def gemspec
27
27
  @gemspec ||= begin
28
28
  file = File.expand_path('../rb_import.gemspec', __FILE__)
29
- puts file.inspect
30
- puts '**'*12
31
29
  eval(File.read(file), binding, file)
32
30
  end
33
31
  end
@@ -1,28 +1,5 @@
1
1
  #include <ruby.h>
2
-
3
- char *load_file_in_str(const char *in) {
4
- FILE *f = fopen(in, "r");
5
-
6
- if(!f) {
7
- return 0;
8
- } else {
9
- fseek(f, 0, SEEK_END);
10
- long fsize = ftell(f);
11
- fseek(f, 0, SEEK_SET);
12
-
13
- char * out = malloc(fsize + 1);
14
- int res = fread(out, fsize, 1, f);
15
-
16
- if(res <= 0) {
17
- return 0;
18
- } else {
19
- fclose(f);
20
-
21
- out[fsize] = 0;
22
- return out;
23
- }
24
- }
25
- }
2
+ #include "rb_import_utils.h"
26
3
 
27
4
  VALUE rb_import(VALUE self, VALUE file_name) {
28
5
  /* try finding the file the same way Ruby's `load`
@@ -31,10 +8,11 @@ VALUE rb_import(VALUE self, VALUE file_name) {
31
8
 
32
9
  if(RTEST(file_location)) {
33
10
  char *file_path = RSTRING_PTR(file_location);
34
- char *file_content = load_file_in_str(file_path);
11
+ char *file_content = rb_import_load_file_in_str(file_path);
35
12
  int result;
36
13
 
37
14
  VALUE obj = rb_eval_string_protect(file_content, &result);
15
+ free(file_content);
38
16
  if (result) {
39
17
  rb_raise(rb_eLoadError, "can't eval imported file %s", RSTRING_PTR(file_location));
40
18
  } else {
@@ -0,0 +1,36 @@
1
+ #include "rb_import_utils.h"
2
+
3
+ long rb_import_get_file_size(FILE *f) {
4
+ /* preserve the current position */
5
+ long current_position = ftell(f);
6
+
7
+ fseek(f, 0, SEEK_END);
8
+ long file_size = ftell(f);
9
+
10
+ /* restore the previous position */
11
+ fseek(f, 0, current_position);
12
+
13
+ return file_size;
14
+ }
15
+
16
+ char *rb_import_read_file(FILE *f) {
17
+ long file_size = rb_import_get_file_size(f);
18
+ char * file_content = malloc(file_size + 1);
19
+ int res = fread(file_content, file_size, 1, f);
20
+
21
+ fclose(f);
22
+ file_content[file_size] = 0;
23
+
24
+ if(res <= 0) {
25
+ free(file_content);
26
+ file_content = 0;
27
+ }
28
+
29
+ return file_content;
30
+ }
31
+
32
+ char *rb_import_load_file_in_str(const char *in) {
33
+ FILE *f = fopen(in, "r");
34
+
35
+ return f ? rb_import_read_file(f) : 0;
36
+ }
@@ -0,0 +1,9 @@
1
+ #ifndef RB_IMPORT_UTILS_H
2
+ #define RB_IMPORT_UTILS_H
3
+ #include <ruby.h>
4
+
5
+ long rb_import_get_file_size(FILE *);
6
+ char *rb_import_read_file(FILE *);
7
+ char *rb_import_load_file_in_str(const char *);
8
+
9
+ #endif
@@ -3,7 +3,7 @@ $:<< 'lib'
3
3
 
4
4
  Gem::Specification.new do |spec|
5
5
  spec.name = "rb_import"
6
- spec.version = "0.1.0"
6
+ spec.version = "0.2.0"
7
7
  spec.authors = ["Franck Verrot"]
8
8
  spec.email = ["franck@verrot.fr"]
9
9
  spec.summary = %q{Add an import method to your Ruby VM}
@@ -6,4 +6,16 @@ class BasicTest < Minitest::Test
6
6
  f = import("./test/fixtures/foo.rb")
7
7
  assert_equal "foo bar", f.new.to_s
8
8
  end
9
+
10
+ def test_missing_file
11
+ error_occured = false
12
+
13
+ begin
14
+ import("unknown_file.rb")
15
+ rescue LoadError => error
16
+ error_occured = true
17
+ end
18
+
19
+ assert error_occured
20
+ end
9
21
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rb_import
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Franck Verrot
@@ -121,6 +121,8 @@ files:
121
121
  - checksums/0.1.0.sha512
122
122
  - ext/rb_import/extconf.rb
123
123
  - ext/rb_import/rb_import.c
124
+ - ext/rb_import/rb_import_utils.c
125
+ - ext/rb_import/rb_import_utils.h
124
126
  - lib/.gitkeep
125
127
  - rb_import.gemspec
126
128
  - test/basic_test.rb
metadata.gz.sig CHANGED
Binary file