Class: Wardite::WasiSnapshotPreview1

Inherits:
Object
  • Object
show all
Includes:
ValueHelper, WasmModule
Defined in:
lib/wardite/wasi.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from WasmModule

#callable, #invoke

Methods included from ValueHelper

#F32, #F64, #I32, #I64

Constructor Details

#initialize(argv: [], mapdir: {}) ⇒ WasiSnapshotPreview1



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/wardite/wasi.rb', line 24

def initialize(argv: [], mapdir: {})
  @fd_table = {
    0 => STDIN,
    1 => STDOUT,
    2 => STDERR,
  }
  @fd_count = 3
  @argv = argv
  @mapdir = mapdir

  @dirent_cache = {}
end

Instance Attribute Details

#argvObject

: Array



17
18
19
# File 'lib/wardite/wasi.rb', line 17

def argv
  @argv
end

#dirent_cacheObject

: Hash[Integer, ::Wardite::Wasi::DirentCache]



19
20
21
# File 'lib/wardite/wasi.rb', line 19

def dirent_cache
  @dirent_cache
end

#fd_countObject

: Integer



16
17
18
# File 'lib/wardite/wasi.rb', line 16

def fd_count
  @fd_count
end

#fd_tableObject

: Hash[Integer, (IO|File|::Wardite::Wasi::PreopenedDir)]



15
16
17
# File 'lib/wardite/wasi.rb', line 15

def fd_table
  @fd_table
end

#mapdirObject

: Hash[String, String]



18
19
20
# File 'lib/wardite/wasi.rb', line 18

def mapdir
  @mapdir
end

Instance Method Details

#args_get(store, args) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/wardite/wasi.rb', line 150

def args_get(store, args)
  arg_offsets_p = args[0].value
  arg_data_buf_p = args[1].value
  if !arg_data_buf_p.is_a?(Integer)
    raise ArgumentError, "invalid type of args: #{args.inspect}"
  end

  arg_offsets = [] #: Array[Integer]
  arg_data_slice = [] #: Array[String]
  current_offset = arg_data_buf_p
  @argv.each do |arg|
    arg_offsets << current_offset
    arg_data_slice << arg
    current_offset += arg.size + 1
  end
  arg_data = arg_data_slice.join("\0") + "\0"

  memory = store.memories[0]
  memory.data[arg_data_buf_p...(arg_data_buf_p + arg_data.size)] = arg_data

  arg_offsets.each_with_index do |offset, i|
    data_begin = arg_offsets_p + i * 4
    memory.data[data_begin...(data_begin + 4)] = [offset].pack("I!")
  end

  0
end

#args_sizes_get(store, args) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
# File 'lib/wardite/wasi.rb', line 181

def args_sizes_get(store, args)
  argc_p = args[0].value
  arglen_p = args[1].value
  argc = @argv.length
  arglen = @argv.map{|arg| arg.size + 1}.sum

  memory = store.memories[0]
  memory.data[argc_p...(argc_p+4)] = [argc].pack("I!")
  memory.data[arglen_p...(arglen_p+4)] = [arglen].pack("I!")
  0
end

#clock_time_get(store, args) ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/wardite/wasi.rb', line 242

def clock_time_get(store, args)
  clock_id = args[0].value
  # we dont use precision...
  _precision = args[1].value
  timebuf64 = args[2].value
  if clock_id != 0 # - CLOCKID_REALTIME
    # raise NotImplementedError, "CLOCKID_REALTIME is an only supported id"
    return Wasi::EINVAL
  end
  # timestamp in nanoseconds
  now = Time.now.to_i * 1_000_000

  memory = store.memories[0]
  now_packed = [now].pack("Q!")
  memory.data[timebuf64...(timebuf64+8)] = now_packed
  0
end

#environ_get(store, args) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/wardite/wasi.rb', line 211

def environ_get(store, args)
  environ_offsets_p = args[0].value
  environ_data_buf_p = args[1].value
  if !environ_data_buf_p.is_a?(Integer)
    raise ArgumentError, "invalid type of args: #{args.inspect}"
  end

  environ_offsets = [] #: Array[Integer]
  environ_data_slice = [] #: Array[String]
  current_offset = environ_data_buf_p
  ENV.each do |k, v|
    environ_offsets << current_offset
    environ_data_slice << "#{k}=#{v}"
    current_offset += "#{k}=#{v}".size + 1
  end
  environ_data = environ_data_slice.join("\0") + "\0"

  memory = store.memories[0]
  memory.data[environ_data_buf_p...(environ_data_buf_p + environ_data.size)] = environ_data

  environ_offsets.each_with_index do |offset, i|
    data_begin = environ_offsets_p + i * 4
    memory.data[data_begin...(data_begin + 4)] = [offset].pack("I!")
  end

  0
end

#environ_sizes_get(store, args) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
# File 'lib/wardite/wasi.rb', line 196

def environ_sizes_get(store, args)
  envc_p = args[0].value
  envlen_p = args[1].value
  envc = ENV.length
  envlen = ENV.map{|k,v| k.size + v.size + 1}.sum

  memory = store.memories[0]
  memory.data[envc_p...(envc_p+4)] = [envc].pack("I!")
  memory.data[envlen_p...(envlen_p+4)] = [envlen].pack("I!")
  0
end

#fd_close(store, args) ⇒ Object



714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
# File 'lib/wardite/wasi.rb', line 714

def fd_close(store, args)
  fd = args[0].value.to_i
  if fd >= fd_count
    return Wasi::EBADF
  end
  file = @fd_table[fd]
  if !file
    return Wasi::EBADF
  end
  if file.is_a?(Wasi::PreopenedDir)
    # do nothing for preopened dir...?
    $stderr.puts "close preopened dir?: #{file.guest_path}"
    @fd_table.delete(fd)
    return 0
  end
  file.close
  @fd_table.delete(fd)
  0
rescue Errno::EBADF
  return Wasi::EBADF      
end

#fd_fdstat_get(store, args) ⇒ Object



578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
# File 'lib/wardite/wasi.rb', line 578

def fd_fdstat_get(store, args)
  fd = args[0].value.to_i
  fdstat_offset = args[1].value.to_i
  if fd >= fd_count
    return Wasi::EBADF
  end
  file = @fd_table[fd]
  fdflags = 0
  if !file
    return Wasi::EBADF
  elsif file.is_a?(Wasi::PreopenedDir)
    file = File.open(file.guest_path) # reopen directory
  elsif file.is_a?(IO) && !file.is_a?(File)
    fdflags |= Wasi::FD_APPEND
  else
    if (Fcntl::O_APPEND & file.fcntl(Fcntl::F_GETFL, 0)) != 0
      fdflags |= Wasi::FD_APPEND
    end
  end

  if (Fcntl::O_NONBLOCK & file.fcntl(Fcntl::F_GETFL, 0)) != 0
    fdflags |= Wasi::FD_NONBLOCK
  end

  stat = file.stat #: File::Stat
  ftype = Wasi.to_ftype(stat.ftype)

  fs_right_base = 0
  fs_right_inheriting = 0

  case ftype
  when Wasi::FILETYPE_DIRECTORY
    fs_right_base = Wasi::RIGHT_DIR_RIGHT_BASE
    fs_right_inheriting = Wasi::RIGHT_FILE_RIGHT_BASE | Wasi::RIGHT_DIR_RIGHT_BASE
  when Wasi::FILETYPE_CHARACTER_DEVICE
    fs_right_base = Wasi::RIGHT_FILE_RIGHT_BASE & \
      (~Wasi::RIGHT_FD_SEEK) & (~Wasi::RIGHT_FD_TELL)
  else
    fs_right_base = Wasi::RIGHT_FILE_RIGHT_BASE
  end

  memory = store.memories[0]

  binformat = [fdflags, ftype, 0, 0, 0, 0, fs_right_base, fs_right_inheriting]
    .pack("SSC4QQ")
  memory.data[fdstat_offset...(fdstat_offset+binformat.size)] = binformat
  0
end

#fd_filestat_get(store, args) ⇒ Object



630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
# File 'lib/wardite/wasi.rb', line 630

def fd_filestat_get(store, args)
  fd = args[0].value.to_i
  filestat_offset = args[1].value.to_i
  if fd >= fd_count
    return Wasi::EBADF
  end
  file = @fd_table[fd]
  if !file || file.is_a?(Wasi::PreopenedDir)
    return Wasi::EBADF
  end

  stat = file.stat #: File::Stat
  memory = store.memories[0]
  binformat = [stat.dev, stat.ino, Wasi.to_ftype(stat.ftype), stat.nlink, stat.size, stat.atime.to_i, stat.mtime.to_i, stat.ctime.to_i].pack("Q8")
  memory.data[filestat_offset...(filestat_offset+binformat.size)] = binformat
  0
end

#fd_prestat_dir_name(store, args) ⇒ Object



483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
# File 'lib/wardite/wasi.rb', line 483

def fd_prestat_dir_name(store, args)
  fd = args[0].value.to_i
  path = args[1].value.to_i
  path_len = args[2].value.to_i
  if fd >= fd_count
    return Wasi::EBADF
  end
  file = @fd_table[fd]
  if !file.is_a?(Wasi::PreopenedDir)
    return Wasi::EBADF
  end
  name = file.path
  if name.size > path_len
    return Wasi::ENAMETOOLONG
  end
  name += ("\0" * (path_len - name.size))

  memory = store.memories[0]
  memory.data[path...(path+name.size)] = name
  0
end

#fd_prestat_get(store, args) ⇒ Object



462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
# File 'lib/wardite/wasi.rb', line 462

def fd_prestat_get(store, args)
  fd = args[0].value.to_i
  prestat_offset = args[1].value.to_i
  if fd >= fd_count
    return Wasi::EBADF
  end
  file = @fd_table[fd]
  if !file.is_a?(Wasi::PreopenedDir)
    return Wasi::EBADF
  end
  name = file.path
  memory = store.memories[0]
  # Zero-value 8-bit tag, and 3-byte zero-value padding
  memory.data[prestat_offset...(prestat_offset+4)] = [0].pack("I!")
  memory.data[(prestat_offset+4)...(prestat_offset+8)] = [name.size].pack("I!")
  0
end

#fd_read(store, args) ⇒ Object



541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
# File 'lib/wardite/wasi.rb', line 541

def fd_read(store, args)
  iargs = args.map do |elm|
    if elm.is_a?(I32)
      elm.value
    else
      raise Wardite::ArgumentError, "invalid type of args: #{args.inspect}"
    end
  end #: Array[Integer]
  fd, iovs, iovs_len, rp = *iargs
  if !fd || !iovs || !iovs_len || !rp
    raise Wardite::ArgumentError, "args too short"
  end
  file = self.fd_table[fd]
  return Wasi::EBADF if !file || file.is_a?(Wasi::PreopenedDir)
  memory = store.memories[0]
  nread = 0
  
  iovs_len.times do
    start = unpack_le_int(memory.data[iovs...(iovs+4)])
    iovs += 4
    slen = unpack_le_int(memory.data[iovs...(iovs+4)])
    iovs += 4
    buf = file.read(slen)
    if !buf
      break 0
    end
    memory.data[start...(start+buf.size)] = buf
    nread += buf.size
  end

  memory.data[rp...(rp+4)] = [nread].pack("I!")
  0
end

#fd_readdir(store, args) ⇒ Object



651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
# File 'lib/wardite/wasi.rb', line 651

def fd_readdir(store, args)
  fd = args[0].value.to_i
  buf = args[1].value.to_i
  buf_len = args[2].value.to_i
  cookie = args[3].value.to_i
  result_buf_used = args[4].value.to_i

  if buf_len < 24 # when smaller than Dirent header size: Q! Q! I! I!
    return Wasi::EINVAL
  end

  memory = store.memories[0]
  
  if dirent_cache[fd]&.eof
    dirent_cache.delete(fd)
  end
  dir = @fd_table[fd]
  if dir.is_a?(Wasi::PreopenedDir) || (dir.is_a?(File) && dir.stat.ftype == "directory")
    dirent_cache[fd] ||= Wasi::DirentCache.new(dir.path)
  else
    return Wasi::EBADF
  end

  dirent = dirent_cache[fd]
  bindata, is_truncated = dirent.fetch_entries_binary(buf_len, cookie)

  bufused = bindata.size
  # bufused == buf_len means more dirents exist
  if is_truncated
    bufused = buf_len
    memory.data[buf...(buf+buf_len)] = bindata + "\0" * (buf_len - bindata.size)
  else
    dirent.eof = true
    memory.data[buf...(buf+bindata.size)] = bindata
  end

  memory.data[result_buf_used...(result_buf_used+4)] = [bufused].pack("I!")
  0
end

#fd_tell(store, args) ⇒ Object



693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
# File 'lib/wardite/wasi.rb', line 693

def fd_tell(store, args)
  fd = args[0].value.to_i
  result_buf = args[1].value.to_i
  if fd >= fd_count
    return Wasi::EBADF
  end
  file = @fd_table[fd]
  if !file || file.is_a?(Wasi::PreopenedDir)
    return Wasi::EBADF
  end

  memory = store.memories[0]
  memory.data[result_buf...(result_buf+4)] = [file.tell].pack("I!")
  0
rescue Errno::EBADF
  return Wasi::EBADF
end

#fd_write(store, args) ⇒ Object



508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
# File 'lib/wardite/wasi.rb', line 508

def fd_write(store, args)
  iargs = args.map do |elm|
    if elm.is_a?(I32)
      elm.value
    else
      raise Wardite::ArgumentError, "invalid type of args: #{args.inspect}"
    end
  end #: Array[Integer]
  fd, iovs, iovs_len, rp = *iargs
  if !fd || !iovs || !iovs_len || !rp
    raise Wardite::ArgumentError, "args too short"
  end
  file = self.fd_table[fd]
  return Wasi::EBADF if !file || file.is_a?(Wasi::PreopenedDir)
  memory = store.memories[0]
  nwritten = 0
  iovs_len.times do
    start = unpack_le_int(memory.data[iovs...(iovs+4)])
    iovs += 4
    slen = unpack_le_int(memory.data[iovs...(iovs+4)])
    iovs += 4
    # TODO: parallel write?
    nwritten += file.write(memory.data[start...(start+slen)])
  end

  memory.data[rp...(rp+4)] = [nwritten].pack("I!")

  0
end

#get_path_at(atfd, target) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/wardite/wasi.rb', line 69

def get_path_at(atfd, target)
  target = resolv_path(target)

  at = @fd_table[atfd]
  pwd = case at
        when Wasi::PreopenedDir
          at.guest_path
        when File
          Dir.fchdir(at.fileno) do
            Dir.pwd
          end
        else
          raise ArgumentError, "invalid fd: #{atfd}"
        end

  ret = File.expand_path(target, pwd)
  ret
end

#interpret_open_flags(dirflags, oflags, fdflags, rights) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/wardite/wasi.rb', line 92

def interpret_open_flags(dirflags, oflags, fdflags, rights)
  open_flags = 0
  if dirflags & Wasi::LOOKUP_SYMLINK_FOLLOW == 0
    open_flags |= File::Constants::NOFOLLOW
  end
  if oflags & Wasi::O_DIRECTORY != 0
    # open_flags |= File::Constants::DIRECTORY
    $stderr.puts "O_DIRECTORY is not supported, ignore" if ENV["WARDITE_TRACE"]
  elsif oflags & Wasi::O_EXCL != 0
    open_flags |= File::Constants::EXCL
  end

  default_mode = File::Constants::RDONLY
  if oflags & Wasi::O_TRUNC != 0
    open_flags |= File::Constants::TRUNC
    default_mode = File::Constants::RDWR
  end
  if oflags & Wasi::O_CREAT != 0
    open_flags |= File::Constants::CREAT
    default_mode = File::Constants::RDWR
  end
  if fdflags & Wasi::FD_NONBLOCK != 0
    open_flags |= File::Constants::NONBLOCK
  end
  if fdflags & Wasi::FD_APPEND != 0
    open_flags |= File::Constants::APPEND
    default_mode = File::Constants::RDWR
  end
  if fdflags & Wasi::FD_DSYNC != 0
    open_flags |= File::Constants::DSYNC
  end
  if fdflags & Wasi::FD_RSYNC != 0
    open_flags |= File::Constants::RSYNC
  end
  if fdflags & Wasi::FD_SYNC != 0
    open_flags |= File::Constants::SYNC
  end

  r = Wasi::RIGHT_FD_READ
  w = Wasi::RIGHT_FD_WRITE
  rw = r | w
  case
  when (rights & rw) == rw
    open_flags |= File::Constants::RDWR
  when (rights & w) == w
    open_flags |= File::Constants::WRONLY
  when (rights & r) == r
    open_flags |= File::Constants::RDONLY
  else
    open_flags |= default_mode
  end

  open_flags
end

#path_create_directory(store, args) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/wardite/wasi.rb', line 263

def path_create_directory(store, args)
  fd = args[0].value.to_i
  path = args[1].value.to_i
  path_len = args[2].value.to_i
  path_str = store.memories[0].data[path...(path+path_len)]
  if !path_str
    return Wasi::ENOENT
  end

  target = get_path_at(fd, path_str)
  Dir.mkdir(target, 0700)
  0
  # TODO; rescue EBADF, ENOTDIR
end

#path_filestat_get(store, args) ⇒ Object



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/wardite/wasi.rb', line 281

def path_filestat_get(store, args)
  fd = args[0].value.to_i
  flags = args[1].value.to_i
  path = args[2].value.to_i
  path_len = args[3].value.to_i
  target = get_path_at(fd, store.memories[0].data[path...(path+path_len)].to_s)

  stat = File.stat(target)
  memory = store.memories[0]
  binformat = [
    stat.dev, stat.ino, Wasi.to_ftype(stat.ftype), stat.nlink,
    stat.size, stat.atime.to_i, stat.mtime.to_i, stat.ctime.to_i
  ].pack("Q8")
  memory.data[flags...(flags+binformat.size)] = binformat
  0
rescue Errno::ENOENT
  return Wasi::ENOENT
end

#path_filestat_set_times(store, args) ⇒ Object



303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/wardite/wasi.rb', line 303

def path_filestat_set_times(store, args)
  fd = args[0].value.to_i
  # TODO: flags support
  _flags = args[1].value.to_i
  path = args[2].value.to_i
  path_len = args[3].value.to_i
  atim = args[4].value.to_i # nanoseconds
  mtim = args[5].value.to_i # nanoseconds
  target = get_path_at(fd, store.memories[0].data[path...(path+path_len)].to_s)

  atime = Time.at(atim.to_f / 1_000_000_000)
  mtime = Time.at(mtim.to_f / 1_000_000_000)
  File.utime(atime, mtime, target)
  0
end


322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/wardite/wasi.rb', line 322

def path_link(store, args)
  old_fd = args[0].value.to_i
  old_path = args[1].value.to_i
  old_path_len = args[2].value.to_i
  old_name = get_path_at(old_fd, store.memories[0].data[old_path...(old_path+old_path_len)].to_s)

  new_fd = args[3].value.to_i
  new_path = args[4].value.to_i
  new_path_len = args[5].value.to_i
  new_name = get_path_at(new_fd, store.memories[0].data[new_path...(new_path+new_path_len)].to_s)

  File.link(old_name, new_name)
  0
end

#path_open(store, args) ⇒ Object



340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/wardite/wasi.rb', line 340

def path_open(store, args)
  dirfd = args[0].value.to_i
  dirflags = args[1].value.to_i
  path = args[2].value.to_i
  path_len = args[3].value.to_i
  oflags = args[4].value.to_i
  fs_rights_base = args[5].value.to_i
  _fs_rights_inheriting = args[6].value.to_i
  fs_flags = args[7].value.to_i
  fd_off = args[8].value.to_i

  path_name = get_path_at(dirfd, store.memories[0].data[path...(path+path_len)].to_s)
  open_flags = interpret_open_flags(dirflags, oflags, fs_flags, fs_rights_base)
  is_dir = (oflags & Wasi::O_DIRECTORY) != 0
  if is_dir && (oflags & Wasi::O_CREAT) != 0
    return Wasi::EINVAL
  end

  file = File.open(path_name, open_flags, 0600)
  fd = set_fd file

  memory = store.memories[0]
  memory.data[fd_off...(fd_off+4)] = [fd].pack("I!")
  0
rescue Errno::ENOENT
  return Wasi::ENOENT
end


371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/wardite/wasi.rb', line 371

def path_readlink(store, args)
  fd = args[0].value.to_i
  path = args[1].value.to_i
  path_len = args[2].value.to_i
  buf = args[3].value.to_i
  buf_len = args[4].value.to_i
  result_buf = args[5].value.to_i

  if buf_len <= 0 || path_len <= 0
    return Wasi::EINVAL
  end
  target = get_path_at(fd, store.memories[0].data[path...(path+path_len)].to_s)

  link_target = File.readlink(target)
  if link_target.size > buf_len
    return Wasi::ENAMETOOLONG
  end

  memory = store.memories[0]
  memory.data[buf...(buf+link_target.size)] = link_target
  memory.data[result_buf...(result_buf+4)] = [link_target.size].pack("I!")
  0
end

#path_remove_directory(store, args) ⇒ Object



398
399
400
401
402
403
404
405
406
407
# File 'lib/wardite/wasi.rb', line 398

def path_remove_directory(store, args)
  fd = args[0].value.to_i
  path = args[1].value.to_i
  path_len = args[2].value.to_i
  path_str = store.memories[0].data[path...(path+path_len)].to_s
  target = get_path_at(fd, path_str)

  Dir.rmdir(target)
  0
end

#path_rename(store, args) ⇒ Object



412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# File 'lib/wardite/wasi.rb', line 412

def path_rename(store, args)
  fd = args[0].value.to_i
  old_path = args[1].value.to_i
  old_path_len = args[2].value.to_i

  new_fd = args[3].value.to_i
  new_path = args[4].value.to_i
  new_path_len = args[5].value.to_i

  old_target = get_path_at(fd, store.memories[0].data[old_path...(old_path+old_path_len)].to_s)
  new_target = get_path_at(new_fd, store.memories[0].data[new_path...(new_path+new_path_len)].to_s)

  File.rename(old_target, new_target)
  0
end


431
432
433
434
435
436
437
438
439
440
441
442
443
# File 'lib/wardite/wasi.rb', line 431

def path_symlink(store, args)
  old_path = args[0].value.to_i
  old_path_len = args[1].value.to_i
  old_name = store.memories[0].data[old_path...(old_path+old_path_len)].to_s

  fd = args[2].value.to_i
  new_path = args[3].value.to_i
  new_path_len = args[4].value.to_i
  new_name = get_path_at(fd, store.memories[0].data[new_path...(new_path+new_path_len)].to_s)

  File.symlink(old_name, new_name)
  0
end


448
449
450
451
452
453
454
455
456
457
# File 'lib/wardite/wasi.rb', line 448

def path_unlink_file(store, args)
  fd = args[0].value.to_i
  path = args[1].value.to_i
  path_len = args[2].value.to_i
  path_str = store.memories[0].data[path...(path+path_len)].to_s
  target = get_path_at(fd, path_str)

  File.unlink(target)
  0
end

#proc_exit(store, args) ⇒ Object



739
740
741
742
# File 'lib/wardite/wasi.rb', line 739

def proc_exit(store, args)
  exit_code = args[0].value
  exit(exit_code)
end

#random_get(store, args) ⇒ Object



747
748
749
750
751
752
753
754
# File 'lib/wardite/wasi.rb', line 747

def random_get(store, args)
  buf = args[0].value.to_i
  buflen = args[1].value.to_i
  randoms = SecureRandom.random_bytes(buflen) #: String
  memory = store.memories[0]
  memory.data[buf...(buf+buflen)] = randoms
  0
end

#resolv_path(orig_path) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/wardite/wasi.rb', line 56

def resolv_path(orig_path)
  @mapdir.each do |k, v|
    if orig_path.start_with?(k)
      return v + orig_path[k.size..-1].to_s
    end
  end

  return orig_path
end

#set_fd(file) ⇒ Object



39
40
41
42
43
44
# File 'lib/wardite/wasi.rb', line 39

def set_fd(file)
  fd = @fd_count
  @fd_table[fd] = file
  @fd_count += 1
  fd
end

#set_preopened_dir(path, guest_path) ⇒ Object



49
50
51
52
# File 'lib/wardite/wasi.rb', line 49

def set_preopened_dir(path, guest_path)
  fd = @fd_count
  set_fd(::Wardite::Wasi::PreopenedDir.new(path, guest_path, fd))
end