SPVM-Sys

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

  [New Features]
    * Add the getcwd method to the Sys::IO class.
    
      static method getcwd : mutable string ($buf : mutable string, $size : int);
      static method realpath : mutable string ($path : string, $resolved_path : mutable string);
      static method _getdcwd : mutable string ($drive : int, $buffer : mutable string, $maxlen : int);
    
    * Add the following methods to the Sys class.
    
      static method getenv : string ($name : string);
      static method setenv : int ($name : string, $value : string, $overwrite : int);
      static method unsetenv : int ($name : string);
    
      
  [Incompatible Changes]
    * Change the definition of the following method in the Sys::IO class.
      [Before]
        static method fwrite : int ($buffer : mutable string, $size : int, $data_length : int, $stream : Sys::IO::FileStream);
      [After]
        static method fwrite : int ($ptr : string, $size : int, $nmemb : int, $stream : Sys::IO::FileStream);
    * Change the definition of the following method in the Sys::IO class.
      [Before]

lib/SPVM/Sys.pm  view on Meta::CPAN

If $value is undef or "", the environment variable is removed.

Exceptions:

This method calls the following methods, so exceptions thrown by these methods could be thrown.

=over 2

=item * L<_putenv_s|SPVM::Sys::Env/"_putenv_s"> in Sys::Env

=item * L<setenv|SPVM::Sys::Env/"setenv"> in Sys::Env

=item * L<unsetenv|SPVM::Sys::Env/"unsetenv"> in Sys::Env

=back

=head2 osname

C<static method osname : string ()>

Gets the OS name. This method corresponds to Perl's L<$^O|https://perldoc.perl.org/perlvar#$%5EO>.

=over 2

lib/SPVM/Sys.spvm  view on Meta::CPAN

    
    if (Sys::OS->is_windows) {
      unless ($value) {
        $value = "";
      }
      Sys::Env->_putenv_s($name, $value);
    }
    else {
      if ($value && $value ne "") {
        my $overwrite = 1;
        Sys::Env->setenv($name, $value, $overwrite);
      }
      else {
        Sys::Env->unsetenv($name);
      }
    }
  }
  
  static method time : long () {
    
    my $time = Sys::Time->time();
    
    return $time;
  }

lib/SPVM/Sys/Env.c  view on Meta::CPAN

  }
  else {
    obj_value = NULL;
  }
  
  stack[0].oval = obj_value;
  
  return 0;
}

int32_t SPVM__Sys__Env__setenv(SPVM_ENV* env, SPVM_VALUE* stack) {
#if defined(_WIN32)
  env->die(env, stack, "Sys::Env#setenv method is not supported in this system(defined(_WIN32)).", __func__, FILE_NAME, __LINE__);
  return SPVM_NATIVE_C_BASIC_TYPE_ID_ERROR_NOT_SUPPORTED_CLASS;
#else
  void* obj_name = stack[0].oval;
  if (!obj_name) {
    return env->die(env, stack, "The environment variable name $name must be defined.", __func__, FILE_NAME, __LINE__);
  }
  const char* name = env->get_chars(env, stack, obj_name);

  void* obj_value = stack[1].oval;
  if (!obj_value) {
    return env->die(env, stack, "The environment variable value $value must be defined.", __func__, FILE_NAME, __LINE__);
  }
  const char* value = env->get_chars(env, stack, obj_value);
  
  int32_t overwrite = stack[2].ival;
  
  int32_t status = setenv(name, value, overwrite);
  
  if (status == -1) {
    env->die(env, stack, "[System Error]setenv() failed:%s.", env->strerror_nolen(env, stack, errno), __func__, FILE_NAME, __LINE__);
    return SPVM_NATIVE_C_BASIC_TYPE_ID_ERROR_SYSTEM_CLASS;
  }
  
  stack[0].ival = status;
  
  return 0;
#endif
}

int32_t SPVM__Sys__Env__unsetenv(SPVM_ENV* env, SPVM_VALUE* stack) {
#if defined(_WIN32)
  env->die(env, stack, "Sys::Env#unsetenv method is not supported in this system(defined(_WIN32)).", __func__, FILE_NAME, __LINE__);
  return SPVM_NATIVE_C_BASIC_TYPE_ID_ERROR_NOT_SUPPORTED_CLASS;
#else
  void* obj_name = stack[0].oval;
  if (!obj_name) {
    return env->die(env, stack, "The environment variable name $name must be defined.", __func__, FILE_NAME, __LINE__);
  }
  const char* name = env->get_chars(env, stack, obj_name);
  
  int32_t status = unsetenv(name);
  
  if (status == -1) {
    env->die(env, stack, "[System Error]unsetenv() failed:%s.", env->strerror_nolen(env, stack, errno), __func__, FILE_NAME, __LINE__);
    return SPVM_NATIVE_C_BASIC_TYPE_ID_ERROR_SYSTEM_CLASS;
  }
  
  stack[0].ival = status;
  
  return 0;
#endif
}

int32_t SPVM__Sys__Env___putenv_s(SPVM_ENV* env, SPVM_VALUE* stack) {

lib/SPVM/Sys/Env.pm  view on Meta::CPAN

=head1 Description

The C<Sys::Env> class in L<SPVM> has methods to manipulate environemnt variables.

=head1 Usage

  use Sys::Env;
  
  my $path = Sys::Env->getenv("PATH");
  
  Sys::Env->setenv("PATH", "/foo/bar");

=head1 Class Methods

=head2 getenv

C<static method getenv : string ($name : string);>

Calls the L<getenv|https://linux.die.net/man/3/getenv> function and copy its return value and returns it.

Exceptions:

$name must be defined. Otherwise an exception is thrown.

=head2 setenv

C<static method setenv : int ($name : string, $value : string, $overwrite : int);>

Calls the L<setenv|https://linux.die.net/man/3/setenv> function and returns its return value.

Exceptions:

$name must be defined. Otherwise an exception is thrown.

$value must be defined. Otherwise an exception is thrown.

If setenv failed, an exception is thrown and C<eval_error_id> is set to the basic type ID of the L<Error::System|SPVM::Error::System>.

In Windows the following exception is thrown. setenv is not supported in this system(defined(_WIN32)).

=head2 unsetenv

C<static method unsetenv : int ($name : string);>

Calls the L<unsetenv|https://linux.die.net/man/3/unsetenv> function and returns its return value.

Exceptions:

$name must be defined. Otherwise an exception is thrown.

If unsetenv failed, an exception is thrown and C<eval_error_id> is set to the basic type ID of the L<Error::System|SPVM::Error::System>.

In Windows the following exception is thrown. unsetenv is not supported in this system(defined(_WIN32)).

=head2 _putenv_s

C<static method _putenv_s : int ($name : string, $value : string);>

Calls the L<_putenv_s|https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/putenv-s-wputenv-s?view=msvc-170> function and returns its return value.

Exceptions:

$name must be defined. Otherwise an exception is thrown.

lib/SPVM/Sys/Env.spvm  view on Meta::CPAN

# Copyright (c) 2023 Yuki Kimoto
# MIT License

class Sys::Env {
  
  native static method getenv : string ($name : string);
  
  native static method setenv : int ($name : string, $value : string, $overwrite : int);
  
  native static method unsetenv : int ($name : string);
  
  native static method _putenv_s : int ($name : string, $value : string);
}



( run in 1.052 second using v1.01-cache-2.11-cpan-6aa56a78535 )