2929 lines
133 KiB
C++
2929 lines
133 KiB
C++
// SPDX-License-Identifier: GPL-2.0-only
|
|||
|
|
// ReMEdy J2ME (MIDP 2.0 / CLDC 1.1) Emulator for Bananarchy Linux
|
||
|
|
// Part 3: CLDC 1.1 Standard Library, RMS, GCF Sockets/HTTP, and ALSA MMAPI Audio
|
||
|
|
|
||
|
|
#ifndef BANANARCHY_REMEDY_CLDC_NET_MEDIA_HPP
|
||
|
|
#define BANANARCHY_REMEDY_CLDC_NET_MEDIA_HPP
|
||
|
|
|
||
|
|
#include "remedy_jvm.hpp"
|
||
|
|
|
||
|
|
#include <alsa/asoundlib.h>
|
||
|
|
#include <arpa/inet.h>
|
||
|
|
#include <atomic>
|
||
|
|
#include <cmath>
|
||
|
|
#include <fcntl.h>
|
||
|
|
#include <mutex>
|
||
|
|
#include <netdb.h>
|
||
|
|
#include <netinet/in.h>
|
||
|
|
#include <netinet/tcp.h>
|
||
|
|
#include <poll.h>
|
||
|
|
#include <random>
|
||
|
|
#include <sys/socket.h>
|
||
|
|
#include <thread>
|
||
|
|
|
||
|
|
namespace remedy {
|
||
|
|
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
// ALSA Low-Latency Tone, MIDI & WAV Audio Engine (JSR-135 MMAPI)
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
class AlsaSoundEngine {
|
||
|
|
public:
|
||
|
|
bool enabled = true;
|
||
|
|
int volumePct = 80;
|
||
|
|
|
||
|
|
~AlsaSoundEngine() { shutdown(); }
|
||
|
|
|
||
|
|
void init() {
|
||
|
|
if (running_.exchange(true)) return;
|
||
|
|
worker_ = std::thread([this]() { audioThreadLoop(); });
|
||
|
|
}
|
||
|
|
|
||
|
|
void shutdown() {
|
||
|
|
if (!running_.exchange(false)) return;
|
||
|
|
if (worker_.joinable()) worker_.join();
|
||
|
|
}
|
||
|
|
|
||
|
|
// Play a MIDI note (0..127, 69 = A4 = 440Hz) for durationMs
|
||
|
|
void playTone(int note, int durationMs, int vol) {
|
||
|
|
if (!enabled || durationMs <= 0 || vol <= 0) return;
|
||
|
|
float freq = 440.0f * std::pow(2.0f, (note - 69) / 12.0f);
|
||
|
|
freq = std::clamp(freq, 55.0f, 8000.0f);
|
||
|
|
int samples = std::clamp((durationMs * 24000) / 1000, 120, 24000 * 10);
|
||
|
|
float amp = std::clamp(vol / 100.0f, 0.05f, 1.0f) *
|
||
|
|
(volumePct / 100.0f) * 0.35f;
|
||
|
|
std::lock_guard<std::mutex> lk(mu_);
|
||
|
|
ActiveVoice v;
|
||
|
|
v.freq = freq;
|
||
|
|
v.phase = 0.0f;
|
||
|
|
v.remainingSamples = samples;
|
||
|
|
v.totalSamples = samples;
|
||
|
|
v.amp = amp;
|
||
|
|
if (voices_.size() < 8) {
|
||
|
|
voices_.push_back(v);
|
||
|
|
} else {
|
||
|
|
voices_[0] = v;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void playWavBytes(const std::vector<uint8_t>& wavData, bool loop = false) {
|
||
|
|
if (!enabled || wavData.size() < 44) return;
|
||
|
|
if (std::memcmp(wavData.data(), "RIFF", 4) != 0 ||
|
||
|
|
std::memcmp(wavData.data() + 8, "WAVE", 4) != 0) {
|
||
|
|
// Fallback: play a pleasant chord if non-WAV (e.g. MIDI header)
|
||
|
|
playTone(60, 120, 80);
|
||
|
|
playTone(64, 140, 80);
|
||
|
|
playTone(67, 180, 80);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
uint16_t channels = readLe16(wavData.data() + 22);
|
||
|
|
uint32_t sampleRate = readLe32(wavData.data() + 24);
|
||
|
|
uint16_t bitsPerSample = readLe16(wavData.data() + 34);
|
||
|
|
if (channels == 0 || sampleRate == 0) return;
|
||
|
|
|
||
|
|
size_t pos = 12;
|
||
|
|
const uint8_t* pcmPtr = nullptr;
|
||
|
|
size_t pcmBytes = 0;
|
||
|
|
while (pos + 8 <= wavData.size()) {
|
||
|
|
uint32_t chunkSz = readLe32(wavData.data() + pos + 4);
|
||
|
|
if (std::memcmp(wavData.data() + pos, "data", 4) == 0) {
|
||
|
|
pcmPtr = wavData.data() + pos + 8;
|
||
|
|
pcmBytes = std::min<size_t>(chunkSz, wavData.size() - (pos + 8));
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
pos += 8 + chunkSz + (chunkSz & 1u);
|
||
|
|
}
|
||
|
|
if (!pcmPtr || pcmBytes == 0) return;
|
||
|
|
|
||
|
|
std::vector<int16_t> resampled;
|
||
|
|
size_t frameSize = channels * (bitsPerSample / 8);
|
||
|
|
if (frameSize == 0) return;
|
||
|
|
size_t totalFrames = pcmBytes / frameSize;
|
||
|
|
size_t outFrames = (totalFrames * 24000ULL) / sampleRate;
|
||
|
|
outFrames = std::min<size_t>(outFrames, 24000 * 30);
|
||
|
|
resampled.resize(outFrames);
|
||
|
|
for (size_t i = 0; i < outFrames; ++i) {
|
||
|
|
size_t srcIdx = (i * sampleRate) / 24000ULL;
|
||
|
|
if (srcIdx >= totalFrames) srcIdx = totalFrames - 1;
|
||
|
|
const uint8_t* fptr = pcmPtr + srcIdx * frameSize;
|
||
|
|
int16_t s = 0;
|
||
|
|
if (bitsPerSample == 16) {
|
||
|
|
s = static_cast<int16_t>(readLe16(fptr));
|
||
|
|
} else if (bitsPerSample == 8) {
|
||
|
|
s = static_cast<int16_t>((static_cast<int>(fptr[0]) - 128) << 8);
|
||
|
|
}
|
||
|
|
resampled[i] = s;
|
||
|
|
}
|
||
|
|
std::lock_guard<std::mutex> lk(mu_);
|
||
|
|
pcmVoice_ = std::move(resampled);
|
||
|
|
pcmPos_ = 0;
|
||
|
|
pcmLoop_ = loop;
|
||
|
|
}
|
||
|
|
|
||
|
|
void stopAll() {
|
||
|
|
std::lock_guard<std::mutex> lk(mu_);
|
||
|
|
voices_.clear();
|
||
|
|
pcmVoice_.clear();
|
||
|
|
pcmPos_ = 0;
|
||
|
|
pcmLoop_ = false;
|
||
|
|
}
|
||
|
|
|
||
|
|
private:
|
||
|
|
struct ActiveVoice {
|
||
|
|
float freq = 440.0f;
|
||
|
|
float phase = 0.0f;
|
||
|
|
int remainingSamples = 0;
|
||
|
|
int totalSamples = 1;
|
||
|
|
float amp = 0.2f;
|
||
|
|
};
|
||
|
|
|
||
|
|
std::atomic<bool> running_{false};
|
||
|
|
std::thread worker_;
|
||
|
|
std::mutex mu_;
|
||
|
|
std::vector<ActiveVoice> voices_;
|
||
|
|
std::vector<int16_t> pcmVoice_;
|
||
|
|
size_t pcmPos_ = 0;
|
||
|
|
bool pcmLoop_ = false;
|
||
|
|
|
||
|
|
void audioThreadLoop() {
|
||
|
|
snd_pcm_t* pcm = nullptr;
|
||
|
|
constexpr unsigned int kRate = 24000;
|
||
|
|
constexpr snd_pcm_uframes_t kPeriod = 512;
|
||
|
|
int16_t stereoBuf[kPeriod * 2];
|
||
|
|
|
||
|
|
while (running_.load()) {
|
||
|
|
bool hasWork = false;
|
||
|
|
{
|
||
|
|
std::lock_guard<std::mutex> lk(mu_);
|
||
|
|
hasWork = !voices_.empty() || (pcmPos_ < pcmVoice_.size());
|
||
|
|
}
|
||
|
|
if (!hasWork || !enabled) {
|
||
|
|
if (pcm) {
|
||
|
|
snd_pcm_drop(pcm);
|
||
|
|
snd_pcm_close(pcm);
|
||
|
|
pcm = nullptr;
|
||
|
|
}
|
||
|
|
::usleep(12000);
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!pcm) {
|
||
|
|
if (snd_pcm_open(&pcm, "default", SND_PCM_STREAM_PLAYBACK, 0) < 0) {
|
||
|
|
if (snd_pcm_open(&pcm, "hw:0,0", SND_PCM_STREAM_PLAYBACK, 0) < 0) {
|
||
|
|
pcm = nullptr;
|
||
|
|
::usleep(50000);
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
snd_pcm_set_params(pcm, SND_PCM_FORMAT_S16_LE,
|
||
|
|
SND_PCM_ACCESS_RW_INTERLEAVED, 2, kRate, 1, 60000);
|
||
|
|
}
|
||
|
|
|
||
|
|
{
|
||
|
|
std::lock_guard<std::mutex> lk(mu_);
|
||
|
|
float masterGain = std::clamp(volumePct / 100.0f, 0.0f, 1.0f);
|
||
|
|
for (size_t i = 0; i < kPeriod; ++i) {
|
||
|
|
float mix = 0.0f;
|
||
|
|
for (auto it = voices_.begin(); it != voices_.end();) {
|
||
|
|
if (it->remainingSamples <= 0) {
|
||
|
|
it = voices_.erase(it);
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
float env = 1.0f;
|
||
|
|
if (it->remainingSamples < 240) {
|
||
|
|
env = it->remainingSamples / 240.0f;
|
||
|
|
}
|
||
|
|
// Rich square+sine retro J2ME synth tone
|
||
|
|
float s = (it->phase < 0.5f ? 0.6f : -0.6f) +
|
||
|
|
0.4f * std::sin(it->phase * 6.2831853f);
|
||
|
|
mix += s * it->amp * env;
|
||
|
|
it->phase += it->freq / static_cast<float>(kRate);
|
||
|
|
if (it->phase >= 1.0f) it->phase -= 1.0f;
|
||
|
|
it->remainingSamples--;
|
||
|
|
++it;
|
||
|
|
}
|
||
|
|
if (pcmPos_ < pcmVoice_.size()) {
|
||
|
|
mix += (pcmVoice_[pcmPos_++] / 32768.0f) * masterGain * 0.8f;
|
||
|
|
if (pcmPos_ >= pcmVoice_.size() && pcmLoop_) {
|
||
|
|
pcmPos_ = 0;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
int32_t sample = static_cast<int32_t>(
|
||
|
|
std::clamp(mix, -0.98f, 0.98f) * 32767.0f);
|
||
|
|
stereoBuf[i * 2] = static_cast<int16_t>(sample);
|
||
|
|
stereoBuf[i * 2 + 1] = static_cast<int16_t>(sample);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
snd_pcm_sframes_t w = snd_pcm_writei(pcm, stereoBuf, kPeriod);
|
||
|
|
if (w < 0) {
|
||
|
|
snd_pcm_recover(pcm, static_cast<int>(w), 1);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if (pcm) {
|
||
|
|
snd_pcm_drop(pcm);
|
||
|
|
snd_pcm_close(pcm);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
// Native Stream, Collection, Timer, and Connection Payloads
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
struct NativeInputStream {
|
||
|
|
std::vector<uint8_t> data;
|
||
|
|
size_t pos = 0;
|
||
|
|
size_t markPos = 0;
|
||
|
|
int sockFd = -1;
|
||
|
|
|
||
|
|
~NativeInputStream() {
|
||
|
|
// Note: sockFd is owned by NativeConnection if shared, only close if standalone
|
||
|
|
}
|
||
|
|
|
||
|
|
int readByte() {
|
||
|
|
if (sockFd >= 0 && pos >= data.size()) {
|
||
|
|
uint8_t b = 0;
|
||
|
|
struct pollfd pfd {sockFd, POLLIN, 0};
|
||
|
|
if (::poll(&pfd, 1, 2000) > 0) {
|
||
|
|
ssize_t n = ::recv(sockFd, &b, 1, 0);
|
||
|
|
if (n == 1) return b;
|
||
|
|
}
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
if (pos >= data.size()) return -1;
|
||
|
|
return data[pos++];
|
||
|
|
}
|
||
|
|
|
||
|
|
int readBytes(uint8_t* dst, int len) {
|
||
|
|
if (len <= 0) return 0;
|
||
|
|
if (sockFd >= 0 && pos >= data.size()) {
|
||
|
|
struct pollfd pfd {sockFd, POLLIN, 0};
|
||
|
|
if (::poll(&pfd, 1, 2500) > 0) {
|
||
|
|
ssize_t n = ::recv(sockFd, dst, len, 0);
|
||
|
|
return (n > 0) ? static_cast<int>(n) : -1;
|
||
|
|
}
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
if (pos >= data.size()) return -1;
|
||
|
|
int avail = static_cast<int>(data.size() - pos);
|
||
|
|
int n = std::min(len, avail);
|
||
|
|
std::memcpy(dst, data.data() + pos, n);
|
||
|
|
pos += n;
|
||
|
|
return n;
|
||
|
|
}
|
||
|
|
|
||
|
|
int available() const {
|
||
|
|
if (sockFd >= 0) {
|
||
|
|
struct pollfd pfd {sockFd, POLLIN, 0};
|
||
|
|
if (::poll(&pfd, 1, 0) > 0) return 1;
|
||
|
|
}
|
||
|
|
return static_cast<int>(data.size() > pos ? (data.size() - pos) : 0);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
struct NativeOutputStream {
|
||
|
|
std::vector<uint8_t> data;
|
||
|
|
int sockFd = -1;
|
||
|
|
|
||
|
|
void writeByte(uint8_t b) {
|
||
|
|
if (sockFd >= 0) {
|
||
|
|
(void)::send(sockFd, &b, 1, MSG_NOSIGNAL);
|
||
|
|
} else {
|
||
|
|
data.push_back(b);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void writeBytes(const uint8_t* src, int len) {
|
||
|
|
if (len <= 0) return;
|
||
|
|
if (sockFd >= 0) {
|
||
|
|
(void)::send(sockFd, src, len, MSG_NOSIGNAL);
|
||
|
|
} else {
|
||
|
|
data.insert(data.end(), src, src + len);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
struct NativeConnection {
|
||
|
|
enum Kind { Socket, Http, File, Datagram, ServerSocket } kind = Socket;
|
||
|
|
std::string url;
|
||
|
|
std::string host;
|
||
|
|
int port = 80;
|
||
|
|
int sockFd = -1;
|
||
|
|
std::string method = "GET";
|
||
|
|
std::map<std::string, std::string> reqHeaders;
|
||
|
|
std::map<std::string, std::string> respHeaders;
|
||
|
|
std::vector<std::pair<std::string, std::string>> respHeaderList;
|
||
|
|
int responseCode = 200;
|
||
|
|
std::string responseMessage = "OK";
|
||
|
|
bool executed = false;
|
||
|
|
std::shared_ptr<NativeInputStream> inStream;
|
||
|
|
std::shared_ptr<NativeOutputStream> outStream;
|
||
|
|
|
||
|
|
~NativeConnection() { close(); }
|
||
|
|
|
||
|
|
void close() {
|
||
|
|
if (sockFd >= 0) {
|
||
|
|
::close(sockFd);
|
||
|
|
sockFd = -1;
|
||
|
|
}
|
||
|
|
if (inStream) inStream->sockFd = -1;
|
||
|
|
if (outStream) outStream->sockFd = -1;
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
struct ScheduledTimerTask {
|
||
|
|
uint32_t taskObjRef = 0;
|
||
|
|
int64_t nextRunMs = 0;
|
||
|
|
int64_t periodMs = 0;
|
||
|
|
bool cancelled = false;
|
||
|
|
};
|
||
|
|
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
// CLDC / RMS / GCF / MMAPI Registration & State
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
class CldcNetMediaRuntime {
|
||
|
|
public:
|
||
|
|
bool networkEnabled = true;
|
||
|
|
AlsaSoundEngine sound;
|
||
|
|
std::vector<ScheduledTimerTask> timerTasks;
|
||
|
|
std::vector<uint32_t> serialCallbacks;
|
||
|
|
|
||
|
|
void init(Jvm& vm) {
|
||
|
|
sound.init();
|
||
|
|
registerJavaLang(vm);
|
||
|
|
registerJavaUtil(vm);
|
||
|
|
registerJavaIo(vm);
|
||
|
|
registerRms(vm);
|
||
|
|
registerGcfNetwork(vm);
|
||
|
|
registerMmapi(vm);
|
||
|
|
}
|
||
|
|
|
||
|
|
void shutdown() {
|
||
|
|
sound.shutdown();
|
||
|
|
}
|
||
|
|
|
||
|
|
void tickTimersAndSerialCallbacks(Jvm& vm) {
|
||
|
|
int64_t now = nowMs();
|
||
|
|
for (size_t i = 0; i < timerTasks.size();) {
|
||
|
|
auto& t = timerTasks[i];
|
||
|
|
const JObject* obj = vm.getObj(t.taskObjRef);
|
||
|
|
if (t.cancelled || !obj || obj->getField("cancelled").i != 0) {
|
||
|
|
timerTasks.erase(timerTasks.begin() + i);
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
if (now >= t.nextRunMs) {
|
||
|
|
vm.invokeMethodSync(t.taskObjRef, "run", "()V", {}, nullptr, 100000);
|
||
|
|
if (t.periodMs > 0 && !t.cancelled) {
|
||
|
|
t.nextRunMs = now + t.periodMs;
|
||
|
|
++i;
|
||
|
|
} else {
|
||
|
|
timerTasks.erase(timerTasks.begin() + i);
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
++i;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!serialCallbacks.empty()) {
|
||
|
|
std::vector<uint32_t> batch = std::move(serialCallbacks);
|
||
|
|
serialCallbacks.clear();
|
||
|
|
for (uint32_t rRef : batch) {
|
||
|
|
if (rRef != 0) {
|
||
|
|
vm.invokeMethodSync(rRef, "run", "()V", {}, nullptr, 150000);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private:
|
||
|
|
static std::shared_ptr<NativeInputStream> getOrCreateInputStream(
|
||
|
|
Jvm& vm, uint32_t ref) {
|
||
|
|
JObject* o = vm.getObj(ref);
|
||
|
|
if (!o) return nullptr;
|
||
|
|
if (!o->nativeData) {
|
||
|
|
o->nativeData = std::make_shared<NativeInputStream>();
|
||
|
|
}
|
||
|
|
return std::static_pointer_cast<NativeInputStream>(o->nativeData);
|
||
|
|
}
|
||
|
|
|
||
|
|
static std::shared_ptr<NativeOutputStream> getOrCreateOutputStream(
|
||
|
|
Jvm& vm, uint32_t ref) {
|
||
|
|
JObject* o = vm.getObj(ref);
|
||
|
|
if (!o) return nullptr;
|
||
|
|
if (!o->nativeData) {
|
||
|
|
o->nativeData = std::make_shared<NativeOutputStream>();
|
||
|
|
}
|
||
|
|
return std::static_pointer_cast<NativeOutputStream>(o->nativeData);
|
||
|
|
}
|
||
|
|
|
||
|
|
void registerJavaLang(Jvm& vm) {
|
||
|
|
// Initialize System.out and System.err static fields
|
||
|
|
uint32_t outStreamRef = vm.allocObject("java/io/PrintStream");
|
||
|
|
JClass* sysCls = vm.loadClass("java/lang/System");
|
||
|
|
if (!sysCls) {
|
||
|
|
// Create synthetic System static storage via setStaticField
|
||
|
|
}
|
||
|
|
(void)outStreamRef;
|
||
|
|
|
||
|
|
// java/lang/Object
|
||
|
|
vm.registerNative("java/lang/Object", "<init>", "()V",
|
||
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) {
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/Object", "getClass", "()Ljava/lang/Class;",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
const JObject* o = v.getObj(a[0].ref);
|
||
|
|
uint32_t cref = v.allocObject("java/lang/Class");
|
||
|
|
if (o) v.getObj(cref)->strUtf8 = o->className;
|
||
|
|
*r = JVal::MakeRef(cref);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/Object", "hashCode", "()I",
|
||
|
|
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
*r = JVal::MakeInt(static_cast<int32_t>(a[0].ref));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/Object", "equals", "(Ljava/lang/Object;)Z",
|
||
|
|
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
*r = JVal::MakeInt(a[0].ref == a[1].ref ? 1 : 0);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/Object", "toString", "()Ljava/lang/String;",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
const JObject* o = v.getObj(a[0].ref);
|
||
|
|
if (!o) {
|
||
|
|
*r = JVal::MakeRef(v.allocString("null"));
|
||
|
|
} else if (o->isString ||
|
||
|
|
o->className == "java/lang/StringBuffer" ||
|
||
|
|
o->className == "java/lang/StringBuilder") {
|
||
|
|
*r = JVal::MakeRef(v.allocString(o->strUtf8));
|
||
|
|
} else {
|
||
|
|
*r = JVal::MakeRef(v.allocString(
|
||
|
|
o->className + "@" + std::to_string(a[0].ref)));
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/Object", "notify", "()V",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||
|
|
v.notifyWaiters(a[0].ref, false);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/Object", "notifyAll", "()V",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||
|
|
v.notifyWaiters(a[0].ref, true);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/Object", "wait", "*",
|
||
|
|
[](Jvm&, JThread& th, const std::vector<JVal>& a, JVal*) {
|
||
|
|
th.state = ThreadState::Waiting;
|
||
|
|
th.waitingOnRef = a[0].ref;
|
||
|
|
int64_t timeout = (a.size() >= 2) ? a[1].l : 0;
|
||
|
|
th.wakeTimeMs = (timeout > 0) ? (nowMs() + timeout)
|
||
|
|
: (nowMs() + 50);
|
||
|
|
th.yieldSlice = true;
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
|
||
|
|
// java/lang/Class
|
||
|
|
vm.registerNative("java/lang/Class", "forName",
|
||
|
|
"(Ljava/lang/String;)Ljava/lang/Class;",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
std::string cname = v.getStringUtf8(a[0].ref);
|
||
|
|
for (char& c : cname) {
|
||
|
|
if (c == '.') c = '/';
|
||
|
|
}
|
||
|
|
v.loadClass(cname);
|
||
|
|
uint32_t cref = v.allocObject("java/lang/Class");
|
||
|
|
v.getObj(cref)->strUtf8 = cname;
|
||
|
|
*r = JVal::MakeRef(cref);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/Class", "newInstance", "()Ljava/lang/Object;",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
const JObject* co = v.getObj(a[0].ref);
|
||
|
|
std::string cname = co ? co->strUtf8 : "java/lang/Object";
|
||
|
|
uint32_t ref = v.allocObject(cname);
|
||
|
|
v.invokeMethodSync(ref, "<init>", "()V");
|
||
|
|
*r = JVal::MakeRef(ref);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/Class", "getName", "()Ljava/lang/String;",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
const JObject* co = v.getObj(a[0].ref);
|
||
|
|
std::string cname = co ? co->strUtf8 : "java.lang.Object";
|
||
|
|
for (char& c : cname) {
|
||
|
|
if (c == '/') c = '.';
|
||
|
|
}
|
||
|
|
*r = JVal::MakeRef(v.allocString(cname));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
"java/lang/Class", "getResourceAsStream",
|
||
|
|
"(Ljava/lang/String;)Ljava/io/InputStream;",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
if (a.size() < 2 || a[1].ref == 0 || !v.jar) {
|
||
|
|
*r = JVal::Null();
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
std::string resPath = v.getStringUtf8(a[1].ref);
|
||
|
|
const JObject* clsObj = v.getObj(a[0].ref);
|
||
|
|
if (!resPath.empty() && resPath[0] != '/' && clsObj &&
|
||
|
|
!clsObj->strUtf8.empty()) {
|
||
|
|
size_t sl = clsObj->strUtf8.rfind('/');
|
||
|
|
if (sl != std::string::npos) {
|
||
|
|
resPath = clsObj->strUtf8.substr(0, sl + 1) + resPath;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
std::vector<uint8_t> bytes;
|
||
|
|
if (!v.jar->extractEntry(resPath, &bytes)) {
|
||
|
|
*r = JVal::Null();
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
uint32_t isRef = v.allocObject("java/io/ByteArrayInputStream");
|
||
|
|
auto nis = getOrCreateInputStream(v, isRef);
|
||
|
|
nis->data = std::move(bytes);
|
||
|
|
*r = JVal::MakeRef(isRef);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
|
||
|
|
// java/lang/System & Runtime
|
||
|
|
vm.registerNative("java/lang/System", "currentTimeMillis", "()J",
|
||
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal* r) {
|
||
|
|
*r = JVal::MakeLong(wallTimeMs());
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/System", "gc", "()V",
|
||
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) {
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/System", "exit", "(I)V",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>&, JVal*) {
|
||
|
|
v.exitRequested = true;
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/System", "identityHashCode",
|
||
|
|
"(Ljava/lang/Object;)I",
|
||
|
|
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
*r = JVal::MakeInt(static_cast<int32_t>(a[0].ref));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
"java/lang/System", "arraycopy",
|
||
|
|
"(Ljava/lang/Object;ILjava/lang/Object;II)V",
|
||
|
|
[](Jvm& v, JThread& th, const std::vector<JVal>& a, JVal*) {
|
||
|
|
JObject* src = v.getObj(a[0].ref);
|
||
|
|
int32_t srcPos = a[1].i;
|
||
|
|
JObject* dst = v.getObj(a[2].ref);
|
||
|
|
int32_t dstPos = a[3].i;
|
||
|
|
int32_t len = a[4].i;
|
||
|
|
if (!src || !dst) {
|
||
|
|
v.throwException(th, "java/lang/NullPointerException");
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
if (len <= 0) return true;
|
||
|
|
if (srcPos < 0 || dstPos < 0 || srcPos + len > src->arrayLength() ||
|
||
|
|
dstPos + len > dst->arrayLength()) {
|
||
|
|
v.throwException(th, "java/lang/ArrayIndexOutOfBoundsException");
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
auto copyVec = [&](auto& sv, auto& dv) {
|
||
|
|
if (&sv == &dv && dstPos > srcPos) {
|
||
|
|
for (int32_t i = len - 1; i >= 0; --i) {
|
||
|
|
dv[dstPos + i] = sv[srcPos + i];
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
for (int32_t i = 0; i < len; ++i) {
|
||
|
|
dv[dstPos + i] = sv[srcPos + i];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
};
|
||
|
|
switch (src->arrayKind) {
|
||
|
|
case ArrayKind::ByteOrBool: copyVec(src->byteArr, dst->byteArr); break;
|
||
|
|
case ArrayKind::Char: copyVec(src->charArr, dst->charArr); break;
|
||
|
|
case ArrayKind::Short: copyVec(src->shortArr, dst->shortArr); break;
|
||
|
|
case ArrayKind::Int: copyVec(src->intArr, dst->intArr); break;
|
||
|
|
case ArrayKind::Long: copyVec(src->longArr, dst->longArr); break;
|
||
|
|
case ArrayKind::Float: copyVec(src->floatArr, dst->floatArr); break;
|
||
|
|
case ArrayKind::Double: copyVec(src->doubleArr, dst->doubleArr); break;
|
||
|
|
case ArrayKind::Ref: copyVec(src->refArr, dst->refArr); break;
|
||
|
|
default: break;
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
"java/lang/System", "getProperty",
|
||
|
|
"(Ljava/lang/String;)Ljava/lang/String;",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
std::string key = v.getStringUtf8(a[0].ref);
|
||
|
|
std::string val;
|
||
|
|
if (key == "microedition.profiles") val = "MIDP-2.0";
|
||
|
|
else if (key == "microedition.configuration") val = "CLDC-1.1";
|
||
|
|
else if (key == "microedition.platform") val = "Nokia8110-4G/ReMEdy-1.0";
|
||
|
|
else if (key == "microedition.encoding") val = "UTF-8";
|
||
|
|
else if (key == "microedition.locale") val = "en-US";
|
||
|
|
else if (key == "microedition.m3g.version") val = "1.1";
|
||
|
|
else if (key == "microedition.io.file.FileConnection.version") val = "1.0";
|
||
|
|
else if (key == "file.separator") val = "/";
|
||
|
|
else if (key == "fileconn.dir.photos") val = "file:///home/user/Pictures/";
|
||
|
|
else if (key == "fileconn.dir.music") val = "file:///home/user/Music/";
|
||
|
|
else if (key == "fileconn.dir.private") val = "file:///home/user/.local/share/sxmo/remedy/files/";
|
||
|
|
else if (key == "com.nokia.mid.batterylevel") val = "90";
|
||
|
|
else if (v.suiteInfo) val = v.suiteInfo->getProperty(key);
|
||
|
|
if (val.empty()) {
|
||
|
|
*r = JVal::Null();
|
||
|
|
} else {
|
||
|
|
*r = JVal::MakeRef(v.allocString(val));
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
|
||
|
|
vm.registerNative("java/lang/Runtime", "getRuntime",
|
||
|
|
"()Ljava/lang/Runtime;",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>&, JVal* r) {
|
||
|
|
*r = JVal::MakeRef(v.allocObject("java/lang/Runtime"));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/Runtime", "freeMemory", "()J",
|
||
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal* r) {
|
||
|
|
*r = JVal::MakeLong(16 * 1024 * 1024LL);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/Runtime", "totalMemory", "()J",
|
||
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal* r) {
|
||
|
|
*r = JVal::MakeLong(32 * 1024 * 1024LL);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/Runtime", "gc", "()V",
|
||
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) {
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
|
||
|
|
// java/lang/String
|
||
|
|
vm.registerNative("java/lang/String", "<init>", "*",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
if (!self) return true;
|
||
|
|
self->isString = true;
|
||
|
|
if (a.size() == 1) {
|
||
|
|
self->strUtf8.clear();
|
||
|
|
} else if (a.size() >= 2) {
|
||
|
|
const JObject* arg1 = v.getObj(a[1].ref);
|
||
|
|
if (!arg1) {
|
||
|
|
self->strUtf8.clear();
|
||
|
|
} else if (arg1->isString ||
|
||
|
|
arg1->className == "java/lang/StringBuffer" ||
|
||
|
|
arg1->className == "java/lang/StringBuilder") {
|
||
|
|
self->strUtf8 = arg1->strUtf8;
|
||
|
|
} else if (arg1->arrayKind == ArrayKind::Char) {
|
||
|
|
int off = (a.size() >= 4) ? a[2].i : 0;
|
||
|
|
int len = (a.size() >= 4)
|
||
|
|
? a[3].i
|
||
|
|
: static_cast<int>(arg1->charArr.size());
|
||
|
|
off = std::clamp(off, 0,
|
||
|
|
static_cast<int>(arg1->charArr.size()));
|
||
|
|
len = std::clamp(
|
||
|
|
len, 0,
|
||
|
|
static_cast<int>(arg1->charArr.size()) - off);
|
||
|
|
self->strUtf8 =
|
||
|
|
Jvm::u16ToUtf8(arg1->charArr.data() + off, len);
|
||
|
|
} else if (arg1->arrayKind == ArrayKind::ByteOrBool) {
|
||
|
|
int off = (a.size() >= 4 && a[2].type == JType::Int)
|
||
|
|
? a[2].i
|
||
|
|
: 0;
|
||
|
|
int len = (a.size() >= 4 && a[3].type == JType::Int)
|
||
|
|
? a[3].i
|
||
|
|
: static_cast<int>(arg1->byteArr.size());
|
||
|
|
off = std::clamp(off, 0,
|
||
|
|
static_cast<int>(arg1->byteArr.size()));
|
||
|
|
len = std::clamp(
|
||
|
|
len, 0,
|
||
|
|
static_cast<int>(arg1->byteArr.size()) - off);
|
||
|
|
self->strUtf8.assign(
|
||
|
|
reinterpret_cast<const char*>(
|
||
|
|
arg1->byteArr.data() + off),
|
||
|
|
len);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/String", "length", "()I",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
auto u16 = Jvm::utf8ToU16(v.getStringUtf8(a[0].ref));
|
||
|
|
*r = JVal::MakeInt(static_cast<int32_t>(u16.size()));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/String", "charAt", "(I)C",
|
||
|
|
[](Jvm& v, JThread& th, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
auto u16 = Jvm::utf8ToU16(v.getStringUtf8(a[0].ref));
|
||
|
|
int32_t idx = a[1].i;
|
||
|
|
if (idx < 0 || idx >= static_cast<int32_t>(u16.size())) {
|
||
|
|
v.throwException(
|
||
|
|
th, "java/lang/IndexOutOfBoundsException");
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
*r = JVal::MakeInt(u16[idx]);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/String", "substring", "(I)Ljava/lang/String;",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
auto u16 = Jvm::utf8ToU16(v.getStringUtf8(a[0].ref));
|
||
|
|
int b = std::clamp<int>(a[1].i, 0, u16.size());
|
||
|
|
*r = JVal::MakeRef(v.allocString(
|
||
|
|
Jvm::u16ToUtf8(u16.data() + b, u16.size() - b)));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/String", "substring", "(II)Ljava/lang/String;",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
auto u16 = Jvm::utf8ToU16(v.getStringUtf8(a[0].ref));
|
||
|
|
int b = std::clamp<int>(a[1].i, 0, u16.size());
|
||
|
|
int e = std::clamp<int>(a[2].i, b, u16.size());
|
||
|
|
*r = JVal::MakeRef(v.allocString(
|
||
|
|
Jvm::u16ToUtf8(u16.data() + b, e - b)));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/String", "equals", "(Ljava/lang/Object;)Z",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
if (a[1].ref == 0) {
|
||
|
|
*r = JVal::MakeInt(0);
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
const JObject* o2 = v.getObj(a[1].ref);
|
||
|
|
if (!o2 || (!o2->isString &&
|
||
|
|
o2->className != "java/lang/String")) {
|
||
|
|
*r = JVal::MakeInt(0);
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
*r = JVal::MakeInt(v.getStringUtf8(a[0].ref) ==
|
||
|
|
v.getStringUtf8(a[1].ref)
|
||
|
|
? 1
|
||
|
|
: 0);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/String", "equalsIgnoreCase",
|
||
|
|
"(Ljava/lang/String;)Z",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
if (a[1].ref == 0) {
|
||
|
|
*r = JVal::MakeInt(0);
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
*r = JVal::MakeInt(
|
||
|
|
toLowerStr(v.getStringUtf8(a[0].ref)) ==
|
||
|
|
toLowerStr(v.getStringUtf8(a[1].ref))
|
||
|
|
? 1
|
||
|
|
: 0);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/String", "compareTo",
|
||
|
|
"(Ljava/lang/String;)I",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
std::string s1 = v.getStringUtf8(a[0].ref);
|
||
|
|
std::string s2 = v.getStringUtf8(a[1].ref);
|
||
|
|
*r = JVal::MakeInt(s1.compare(s2));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/String", "startsWith", "*",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
std::string s = v.getStringUtf8(a[0].ref);
|
||
|
|
std::string p = v.getStringUtf8(a[1].ref);
|
||
|
|
int off = (a.size() >= 3) ? std::max(0, a[2].i) : 0;
|
||
|
|
if (static_cast<size_t>(off) > s.size()) {
|
||
|
|
*r = JVal::MakeInt(0);
|
||
|
|
} else {
|
||
|
|
*r = JVal::MakeInt(s.rfind(p, off) ==
|
||
|
|
static_cast<size_t>(off)
|
||
|
|
? 1
|
||
|
|
: 0);
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/String", "endsWith",
|
||
|
|
"(Ljava/lang/String;)Z",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
std::string s = v.getStringUtf8(a[0].ref);
|
||
|
|
std::string p = v.getStringUtf8(a[1].ref);
|
||
|
|
bool ok = s.size() >= p.size() &&
|
||
|
|
s.compare(s.size() - p.size(), p.size(), p) == 0;
|
||
|
|
*r = JVal::MakeInt(ok ? 1 : 0);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/String", "indexOf", "*",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
std::string s = v.getStringUtf8(a[0].ref);
|
||
|
|
size_t from = (a.size() >= 3)
|
||
|
|
? static_cast<size_t>(std::max(0, a[2].i))
|
||
|
|
: 0;
|
||
|
|
size_t pos = std::string::npos;
|
||
|
|
if (a[1].type == JType::Ref) {
|
||
|
|
pos = s.find(v.getStringUtf8(a[1].ref), from);
|
||
|
|
} else {
|
||
|
|
pos = s.find(static_cast<char>(a[1].i), from);
|
||
|
|
}
|
||
|
|
*r = JVal::MakeInt(pos == std::string::npos
|
||
|
|
? -1
|
||
|
|
: static_cast<int32_t>(pos));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/String", "lastIndexOf", "*",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
std::string s = v.getStringUtf8(a[0].ref);
|
||
|
|
size_t from = (a.size() >= 3)
|
||
|
|
? static_cast<size_t>(std::max(0, a[2].i))
|
||
|
|
: std::string::npos;
|
||
|
|
size_t pos = std::string::npos;
|
||
|
|
if (a[1].type == JType::Ref) {
|
||
|
|
pos = s.rfind(v.getStringUtf8(a[1].ref), from);
|
||
|
|
} else {
|
||
|
|
pos = s.rfind(static_cast<char>(a[1].i), from);
|
||
|
|
}
|
||
|
|
*r = JVal::MakeInt(pos == std::string::npos
|
||
|
|
? -1
|
||
|
|
: static_cast<int32_t>(pos));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/String", "trim", "()Ljava/lang/String;",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
*r = JVal::MakeRef(
|
||
|
|
v.allocString(trimStr(v.getStringUtf8(a[0].ref))));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/String", "toLowerCase",
|
||
|
|
"()Ljava/lang/String;",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
*r = JVal::MakeRef(
|
||
|
|
v.allocString(toLowerStr(v.getStringUtf8(a[0].ref))));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/String", "toUpperCase",
|
||
|
|
"()Ljava/lang/String;",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
std::string s = v.getStringUtf8(a[0].ref);
|
||
|
|
for (char& c : s) {
|
||
|
|
c = static_cast<char>(
|
||
|
|
std::toupper(static_cast<unsigned char>(c)));
|
||
|
|
}
|
||
|
|
*r = JVal::MakeRef(v.allocString(s));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/String", "replace", "(CC)Ljava/lang/String;",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
auto u16 = Jvm::utf8ToU16(v.getStringUtf8(a[0].ref));
|
||
|
|
uint16_t oldC = static_cast<uint16_t>(a[1].i);
|
||
|
|
uint16_t newC = static_cast<uint16_t>(a[2].i);
|
||
|
|
for (uint16_t& c : u16) {
|
||
|
|
if (c == oldC) c = newC;
|
||
|
|
}
|
||
|
|
*r = JVal::MakeRef(v.allocString(
|
||
|
|
Jvm::u16ToUtf8(u16.data(), u16.size())));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/String", "concat",
|
||
|
|
"(Ljava/lang/String;)Ljava/lang/String;",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
*r = JVal::MakeRef(v.allocString(
|
||
|
|
v.getStringUtf8(a[0].ref) +
|
||
|
|
v.getStringUtf8(a[1].ref)));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/String", "getBytes", "*",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
std::string s = v.getStringUtf8(a[0].ref);
|
||
|
|
uint32_t arr = v.allocPrimitiveArray(
|
||
|
|
8, static_cast<int32_t>(s.size()));
|
||
|
|
JObject* ao = v.getObj(arr);
|
||
|
|
for (size_t i = 0; i < s.size(); ++i) {
|
||
|
|
ao->byteArr[i] = static_cast<int8_t>(s[i]);
|
||
|
|
}
|
||
|
|
*r = JVal::MakeRef(arr);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/String", "toCharArray", "()[C",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
auto u16 = Jvm::utf8ToU16(v.getStringUtf8(a[0].ref));
|
||
|
|
uint32_t arr = v.allocPrimitiveArray(
|
||
|
|
5, static_cast<int32_t>(u16.size()));
|
||
|
|
v.getObj(arr)->charArr = std::move(u16);
|
||
|
|
*r = JVal::MakeRef(arr);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/String", "hashCode", "()I",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
auto u16 = Jvm::utf8ToU16(v.getStringUtf8(a[0].ref));
|
||
|
|
uint32_t h = 0;
|
||
|
|
for (uint16_t c : u16) h = 31u * h + c;
|
||
|
|
*r = JVal::MakeInt(static_cast<int32_t>(h));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/String", "intern", "()Ljava/lang/String;",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
*r = JVal::MakeRef(
|
||
|
|
v.internString(v.getStringUtf8(a[0].ref)));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/String", "valueOf", "*",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
if (a.empty()) {
|
||
|
|
*r = JVal::MakeRef(v.allocString(""));
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
const JVal& x = a[0];
|
||
|
|
if (x.type == JType::Ref) {
|
||
|
|
if (x.ref == 0) {
|
||
|
|
*r = JVal::MakeRef(v.allocString("null"));
|
||
|
|
} else {
|
||
|
|
const JObject* o = v.getObj(x.ref);
|
||
|
|
if (o && o->arrayKind == ArrayKind::Char) {
|
||
|
|
int off = (a.size() >= 3) ? a[1].i : 0;
|
||
|
|
int len = (a.size() >= 3)
|
||
|
|
? a[2].i
|
||
|
|
: static_cast<int>(o->charArr.size());
|
||
|
|
off = std::clamp(
|
||
|
|
off, 0, static_cast<int>(o->charArr.size()));
|
||
|
|
len = std::clamp(
|
||
|
|
len, 0,
|
||
|
|
static_cast<int>(o->charArr.size()) - off);
|
||
|
|
*r = JVal::MakeRef(v.allocString(
|
||
|
|
Jvm::u16ToUtf8(o->charArr.data() + off, len)));
|
||
|
|
} else {
|
||
|
|
*r = JVal::MakeRef(
|
||
|
|
v.allocString(v.getStringUtf8(x.ref)));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
} else if (x.type == JType::Long) {
|
||
|
|
*r = JVal::MakeRef(v.allocString(std::to_string(x.l)));
|
||
|
|
} else if (x.type == JType::Float) {
|
||
|
|
char buf[64];
|
||
|
|
std::snprintf(buf, sizeof(buf), "%g", x.f);
|
||
|
|
*r = JVal::MakeRef(v.allocString(buf));
|
||
|
|
} else if (x.type == JType::Double) {
|
||
|
|
char buf[64];
|
||
|
|
std::snprintf(buf, sizeof(buf), "%g", x.d);
|
||
|
|
*r = JVal::MakeRef(v.allocString(buf));
|
||
|
|
} else {
|
||
|
|
*r = JVal::MakeRef(v.allocString(std::to_string(x.i)));
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
|
||
|
|
// java/lang/StringBuffer & StringBuilder
|
||
|
|
for (const char* sbCls :
|
||
|
|
{"java/lang/StringBuffer", "java/lang/StringBuilder"}) {
|
||
|
|
vm.registerNative(sbCls, "<init>", "*",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
if (self && a.size() >= 2 &&
|
||
|
|
a[1].type == JType::Ref && a[1].ref != 0) {
|
||
|
|
self->strUtf8 = v.getStringUtf8(a[1].ref);
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
sbCls, "append", "(C)Ljava/lang/StringBuffer;",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
uint16_t ch = static_cast<uint16_t>(a[1].i);
|
||
|
|
if (self) self->strUtf8 += Jvm::u16ToUtf8(&ch, 1);
|
||
|
|
*r = a[0];
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
sbCls, "append", "(C)Ljava/lang/StringBuilder;",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
uint16_t ch = static_cast<uint16_t>(a[1].i);
|
||
|
|
if (self) self->strUtf8 += Jvm::u16ToUtf8(&ch, 1);
|
||
|
|
*r = a[0];
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
sbCls, "append", "(Z)Ljava/lang/StringBuffer;",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
if (self) self->strUtf8 += (a[1].i != 0 ? "true" : "false");
|
||
|
|
*r = a[0];
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
sbCls, "append", "(Z)Ljava/lang/StringBuilder;",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
if (self) self->strUtf8 += (a[1].i != 0 ? "true" : "false");
|
||
|
|
*r = a[0];
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
sbCls, "append", "*",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
if (self && a.size() >= 2) {
|
||
|
|
const JVal& x = a[1];
|
||
|
|
if (x.type == JType::Ref) {
|
||
|
|
if (x.ref == 0) {
|
||
|
|
self->strUtf8 += "null";
|
||
|
|
} else {
|
||
|
|
const JObject* o = v.getObj(x.ref);
|
||
|
|
if (o && o->arrayKind == ArrayKind::Char) {
|
||
|
|
int off = (a.size() >= 4) ? a[2].i : 0;
|
||
|
|
int len = (a.size() >= 4)
|
||
|
|
? a[3].i
|
||
|
|
: static_cast<int>(o->charArr.size());
|
||
|
|
off = std::clamp(off, 0,
|
||
|
|
static_cast<int>(o->charArr.size()));
|
||
|
|
len = std::clamp(
|
||
|
|
len, 0, static_cast<int>(o->charArr.size()) - off);
|
||
|
|
self->strUtf8 +=
|
||
|
|
Jvm::u16ToUtf8(o->charArr.data() + off, len);
|
||
|
|
} else {
|
||
|
|
self->strUtf8 += v.getStringUtf8(x.ref);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
} else if (x.type == JType::Long) {
|
||
|
|
self->strUtf8 += std::to_string(x.l);
|
||
|
|
} else if (x.type == JType::Float) {
|
||
|
|
char buf[64];
|
||
|
|
std::snprintf(buf, sizeof(buf), "%g", x.f);
|
||
|
|
self->strUtf8 += buf;
|
||
|
|
} else if (x.type == JType::Double) {
|
||
|
|
char buf[64];
|
||
|
|
std::snprintf(buf, sizeof(buf), "%g", x.d);
|
||
|
|
self->strUtf8 += buf;
|
||
|
|
} else {
|
||
|
|
self->strUtf8 += std::to_string(x.i);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
*r = a[0];
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(sbCls, "length", "()I",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
auto u16 = Jvm::utf8ToU16(self ? self->strUtf8 : "");
|
||
|
|
*r = JVal::MakeInt(static_cast<int32_t>(u16.size()));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(sbCls, "setLength", "(I)V",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
if (!self) return true;
|
||
|
|
auto u16 = Jvm::utf8ToU16(self->strUtf8);
|
||
|
|
int n = std::max(0, a[1].i);
|
||
|
|
u16.resize(n, 0);
|
||
|
|
self->strUtf8 = Jvm::u16ToUtf8(u16.data(), u16.size());
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(sbCls, "charAt", "(I)C",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
auto u16 = Jvm::utf8ToU16(self ? self->strUtf8 : "");
|
||
|
|
int idx = a[1].i;
|
||
|
|
*r = JVal::MakeInt(
|
||
|
|
(idx >= 0 && idx < static_cast<int>(u16.size()))
|
||
|
|
? u16[idx]
|
||
|
|
: 0);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(sbCls, "delete", "*",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
if (self) {
|
||
|
|
auto u16 = Jvm::utf8ToU16(self->strUtf8);
|
||
|
|
int s = std::clamp<int>(a[1].i, 0, u16.size());
|
||
|
|
int e = std::clamp<int>(a[2].i, s, u16.size());
|
||
|
|
u16.erase(u16.begin() + s, u16.begin() + e);
|
||
|
|
self->strUtf8 =
|
||
|
|
Jvm::u16ToUtf8(u16.data(), u16.size());
|
||
|
|
}
|
||
|
|
*r = a[0];
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(sbCls, "toString", "()Ljava/lang/String;",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
*r = JVal::MakeRef(
|
||
|
|
v.allocString(self ? self->strUtf8 : ""));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
// Boxed primitive classes (Integer, Long, Short, Byte, Boolean, Character, Float, Double)
|
||
|
|
for (const char* bCls :
|
||
|
|
{"java/lang/Integer", "java/lang/Short", "java/lang/Byte",
|
||
|
|
"java/lang/Boolean", "java/lang/Character"}) {
|
||
|
|
vm.registerNative(bCls, "<init>", "*",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
if (self && a.size() >= 2) {
|
||
|
|
if (a[1].type == JType::Ref) {
|
||
|
|
self->setField(
|
||
|
|
"value",
|
||
|
|
JVal::MakeInt(std::atoi(
|
||
|
|
v.getStringUtf8(a[1].ref).c_str())));
|
||
|
|
} else {
|
||
|
|
self->setField("value", JVal::MakeInt(a[1].i));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
for (const char* mName :
|
||
|
|
{"intValue", "shortValue", "byteValue", "booleanValue", "charValue"}) {
|
||
|
|
vm.registerNative(bCls, mName, "*",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a,
|
||
|
|
JVal* r) {
|
||
|
|
const JObject* self = v.getObj(a[0].ref);
|
||
|
|
*r = JVal::MakeInt(self ? self->getField("value").i
|
||
|
|
: 0);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
}
|
||
|
|
vm.registerNative(bCls, "toString", "()Ljava/lang/String;",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
const JObject* self = v.getObj(a[0].ref);
|
||
|
|
int32_t val = self ? self->getField("value").i : 0;
|
||
|
|
if (self && self->className == "java/lang/Boolean") {
|
||
|
|
*r = JVal::MakeRef(
|
||
|
|
v.allocString(val != 0 ? "true" : "false"));
|
||
|
|
} else {
|
||
|
|
*r = JVal::MakeRef(
|
||
|
|
v.allocString(std::to_string(val)));
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
}
|
||
|
|
vm.registerNative("java/lang/Integer", "toString", "*",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
if (a[0].type == JType::Ref) {
|
||
|
|
const JObject* self = v.getObj(a[0].ref);
|
||
|
|
*r = JVal::MakeRef(v.allocString(
|
||
|
|
std::to_string(self ? self->getField("value").i : 0)));
|
||
|
|
} else {
|
||
|
|
int radix = (a.size() >= 2) ? a[1].i : 10;
|
||
|
|
if (radix == 16) {
|
||
|
|
char buf[32];
|
||
|
|
std::snprintf(buf, sizeof(buf), "%x", a[0].i);
|
||
|
|
*r = JVal::MakeRef(v.allocString(buf));
|
||
|
|
} else {
|
||
|
|
*r = JVal::MakeRef(
|
||
|
|
v.allocString(std::to_string(a[0].i)));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/Integer", "toHexString",
|
||
|
|
"(I)Ljava/lang/String;",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
char buf[32];
|
||
|
|
std::snprintf(buf, sizeof(buf), "%x",
|
||
|
|
static_cast<uint32_t>(a[0].i));
|
||
|
|
*r = JVal::MakeRef(v.allocString(buf));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/Integer", "parseInt", "*",
|
||
|
|
[](Jvm& v, JThread& th, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
std::string s = trimStr(v.getStringUtf8(a[0].ref));
|
||
|
|
int radix = (a.size() >= 2) ? a[1].i : 10;
|
||
|
|
if (s.empty()) {
|
||
|
|
v.throwException(th, "java/lang/NumberFormatException");
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
char* endp = nullptr;
|
||
|
|
long val = std::strtol(s.c_str(), &endp, radix);
|
||
|
|
*r = JVal::MakeInt(static_cast<int32_t>(val));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/Integer", "valueOf", "*",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
int32_t val = (a[0].type == JType::Ref)
|
||
|
|
? std::atoi(
|
||
|
|
v.getStringUtf8(a[0].ref).c_str())
|
||
|
|
: a[0].i;
|
||
|
|
uint32_t ref = v.allocObject("java/lang/Integer");
|
||
|
|
v.getObj(ref)->setField("value", JVal::MakeInt(val));
|
||
|
|
*r = JVal::MakeRef(ref);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/Long", "<init>", "(J)V",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
if (self) self->setField("value", JVal::MakeLong(a[1].l));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/Long", "longValue", "()J",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
const JObject* self = v.getObj(a[0].ref);
|
||
|
|
*r = JVal::MakeLong(self ? self->getField("value", 'J').l
|
||
|
|
: 0);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/Long", "parseLong", "*",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
std::string s = trimStr(v.getStringUtf8(a[0].ref));
|
||
|
|
int radix = (a.size() >= 2) ? a[1].i : 10;
|
||
|
|
*r = JVal::MakeLong(std::strtoll(s.c_str(), nullptr, radix));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/Long", "toString", "*",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
int64_t val = (a[0].type == JType::Ref)
|
||
|
|
? v.getObj(a[0].ref)->getField("value", 'J').l
|
||
|
|
: a[0].l;
|
||
|
|
*r = JVal::MakeRef(v.allocString(std::to_string(val)));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
|
||
|
|
// java/lang/Math
|
||
|
|
vm.registerNative("java/lang/Math", "abs", "(I)I",
|
||
|
|
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
*r = JVal::MakeInt(std::abs(a[0].i));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/Math", "abs", "(J)J",
|
||
|
|
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
*r = JVal::MakeLong(std::llabs(a[0].l));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/Math", "abs", "(F)F",
|
||
|
|
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
*r = JVal::MakeFloat(std::fabs(a[0].f));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/Math", "abs", "(D)D",
|
||
|
|
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
*r = JVal::MakeDouble(std::fabs(a[0].d));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/Math", "min", "(II)I",
|
||
|
|
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
*r = JVal::MakeInt(std::min(a[0].i, a[1].i));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/Math", "min", "(JJ)J",
|
||
|
|
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
*r = JVal::MakeLong(std::min(a[0].l, a[1].l));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/Math", "min", "(FF)F",
|
||
|
|
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
*r = JVal::MakeFloat(std::min(a[0].f, a[1].f));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/Math", "min", "(DD)D",
|
||
|
|
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
*r = JVal::MakeDouble(std::min(a[0].d, a[1].d));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/Math", "max", "(II)I",
|
||
|
|
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
*r = JVal::MakeInt(std::max(a[0].i, a[1].i));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/Math", "max", "(JJ)J",
|
||
|
|
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
*r = JVal::MakeLong(std::max(a[0].l, a[1].l));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/Math", "max", "(FF)F",
|
||
|
|
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
*r = JVal::MakeFloat(std::max(a[0].f, a[1].f));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/Math", "max", "(DD)D",
|
||
|
|
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
*r = JVal::MakeDouble(std::max(a[0].d, a[1].d));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/Math", "sin", "(D)D",
|
||
|
|
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
*r = JVal::MakeDouble(std::sin(a[0].d));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/Math", "cos", "(D)D",
|
||
|
|
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
*r = JVal::MakeDouble(std::cos(a[0].d));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/Math", "tan", "(D)D",
|
||
|
|
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
*r = JVal::MakeDouble(std::tan(a[0].d));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/Math", "sqrt", "(D)D",
|
||
|
|
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
*r = JVal::MakeDouble(std::sqrt(a[0].d));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/Math", "ceil", "(D)D",
|
||
|
|
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
*r = JVal::MakeDouble(std::ceil(a[0].d));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/Math", "floor", "(D)D",
|
||
|
|
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
*r = JVal::MakeDouble(std::floor(a[0].d));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/Math", "toRadians", "(D)D",
|
||
|
|
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
*r = JVal::MakeDouble(a[0].d * 0.017453292519943295);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/Math", "toDegrees", "(D)D",
|
||
|
|
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
*r = JVal::MakeDouble(a[0].d * 57.29577951308232);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
|
||
|
|
// java/lang/Thread
|
||
|
|
vm.registerNative("java/lang/Thread", "<init>", "*",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
if (self && a.size() >= 2 && a[1].type == JType::Ref) {
|
||
|
|
const JObject* arg1 = v.getObj(a[1].ref);
|
||
|
|
if (arg1 && !arg1->isString) {
|
||
|
|
self->setField("target", a[1]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/Thread", "start", "()V",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
if (!self) return true;
|
||
|
|
uint32_t runRef = self->getField("target", 'L').ref;
|
||
|
|
if (runRef == 0) runRef = a[0].ref;
|
||
|
|
JObject* runObj = v.getObj(runRef);
|
||
|
|
if (!runObj) return true;
|
||
|
|
JClass* cls = nullptr;
|
||
|
|
const JMethodInfo* m = nullptr;
|
||
|
|
if (v.resolveBytecodeMethod(runObj->className, "run",
|
||
|
|
"()V", &cls, &m)) {
|
||
|
|
JThread* nth = v.createThread(a[0].ref);
|
||
|
|
v.pushBytecodeFrame(*nth, cls, m,
|
||
|
|
{JVal::MakeRef(runRef)});
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/Thread", "sleep", "(J)V",
|
||
|
|
[](Jvm&, JThread& th, const std::vector<JVal>& a, JVal*) {
|
||
|
|
int64_t ms = std::max<int64_t>(1, a[0].l);
|
||
|
|
th.state = ThreadState::Sleeping;
|
||
|
|
th.wakeTimeMs = nowMs() + ms;
|
||
|
|
th.yieldSlice = true;
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/Thread", "yield", "()V",
|
||
|
|
[](Jvm&, JThread& th, const std::vector<JVal>&, JVal*) {
|
||
|
|
th.yieldSlice = true;
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/Thread", "currentThread",
|
||
|
|
"()Ljava/lang/Thread;",
|
||
|
|
[](Jvm& v, JThread& th, const std::vector<JVal>&, JVal* r) {
|
||
|
|
if (th.threadObjRef == 0) {
|
||
|
|
th.threadObjRef = v.allocObject("java/lang/Thread");
|
||
|
|
}
|
||
|
|
*r = JVal::MakeRef(th.threadObjRef);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/Thread", "isAlive", "()Z",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
for (const auto& t : v.threads()) {
|
||
|
|
if (t->threadObjRef == a[0].ref &&
|
||
|
|
t->state != ThreadState::Finished) {
|
||
|
|
*r = JVal::MakeInt(1);
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
*r = JVal::MakeInt(0);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
|
||
|
|
// java/lang/Throwable
|
||
|
|
vm.registerNative("java/lang/Throwable", "<init>", "*",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
if (self && a.size() >= 2 && a[1].type == JType::Ref) {
|
||
|
|
self->setField("detailMessage", a[1]);
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/Throwable", "getMessage",
|
||
|
|
"()Ljava/lang/String;",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
const JObject* self = v.getObj(a[0].ref);
|
||
|
|
*r = self ? self->getField("detailMessage", 'L')
|
||
|
|
: JVal::Null();
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/lang/Throwable", "printStackTrace", "()V",
|
||
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) {
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
void registerJavaUtil(Jvm& vm) {
|
||
|
|
// java/util/Random
|
||
|
|
vm.registerNative("java/util/Random", "<init>", "*",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
if (!self) return true;
|
||
|
|
uint64_t seed = (a.size() >= 2)
|
||
|
|
? static_cast<uint64_t>(a[1].l)
|
||
|
|
: static_cast<uint64_t>(wallTimeMs());
|
||
|
|
self->nativeData = std::make_shared<std::mt19937_64>(seed);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/util/Random", "setSeed", "(J)V",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
if (self) {
|
||
|
|
self->nativeData = std::make_shared<std::mt19937_64>(
|
||
|
|
static_cast<uint64_t>(a[1].l));
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/util/Random", "nextInt", "()I",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
if (self && !self->nativeData) {
|
||
|
|
self->nativeData = std::make_shared<std::mt19937_64>(
|
||
|
|
static_cast<uint64_t>(wallTimeMs()));
|
||
|
|
}
|
||
|
|
auto* rng = self ? static_cast<std::mt19937_64*>(
|
||
|
|
self->nativeData.get())
|
||
|
|
: nullptr;
|
||
|
|
*r = JVal::MakeInt(
|
||
|
|
rng ? static_cast<int32_t>((*rng)()) : std::rand());
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/util/Random", "nextInt", "(I)I",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
int32_t bound = a[1].i;
|
||
|
|
if (bound <= 0) {
|
||
|
|
*r = JVal::MakeInt(0);
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
if (self && !self->nativeData) {
|
||
|
|
self->nativeData = std::make_shared<std::mt19937_64>(
|
||
|
|
static_cast<uint64_t>(wallTimeMs()));
|
||
|
|
}
|
||
|
|
auto* rng = self ? static_cast<std::mt19937_64*>(
|
||
|
|
self->nativeData.get())
|
||
|
|
: nullptr;
|
||
|
|
uint32_t raw = rng ? static_cast<uint32_t>((*rng)())
|
||
|
|
: static_cast<uint32_t>(std::rand());
|
||
|
|
*r = JVal::MakeInt(
|
||
|
|
static_cast<int32_t>(raw % static_cast<uint32_t>(bound)));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/util/Random", "nextLong", "()J",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
if (self && !self->nativeData) {
|
||
|
|
self->nativeData = std::make_shared<std::mt19937_64>(
|
||
|
|
static_cast<uint64_t>(wallTimeMs()));
|
||
|
|
}
|
||
|
|
auto* rng = self ? static_cast<std::mt19937_64*>(
|
||
|
|
self->nativeData.get())
|
||
|
|
: nullptr;
|
||
|
|
*r = JVal::MakeLong(
|
||
|
|
rng ? static_cast<int64_t>((*rng)()) : std::rand());
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
|
||
|
|
// java/util/Vector & Stack (stored in JObject::refArr)
|
||
|
|
for (const char* vCls : {"java/util/Vector", "java/util/Stack"}) {
|
||
|
|
vm.registerNative(vCls, "<init>", "*",
|
||
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) {
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(vCls, "addElement", "(Ljava/lang/Object;)V",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
if (self) self->refArr.push_back(a[1].ref);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(vCls, "insertElementAt", "(Ljava/lang/Object;I)V",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
if (self) {
|
||
|
|
int idx = std::clamp<int>(a[2].i, 0,
|
||
|
|
self->refArr.size());
|
||
|
|
self->refArr.insert(self->refArr.begin() + idx,
|
||
|
|
a[1].ref);
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(vCls, "setElementAt", "(Ljava/lang/Object;I)V",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
if (self && a[2].i >= 0 &&
|
||
|
|
a[2].i < static_cast<int>(self->refArr.size())) {
|
||
|
|
self->refArr[a[2].i] = a[1].ref;
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(vCls, "elementAt", "(I)Ljava/lang/Object;",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
if (self && a[1].i >= 0 &&
|
||
|
|
a[1].i < static_cast<int>(self->refArr.size())) {
|
||
|
|
*r = JVal::MakeRef(self->refArr[a[1].i]);
|
||
|
|
} else {
|
||
|
|
*r = JVal::Null();
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(vCls, "firstElement", "()Ljava/lang/Object;",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
*r = (self && !self->refArr.empty())
|
||
|
|
? JVal::MakeRef(self->refArr.front())
|
||
|
|
: JVal::Null();
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(vCls, "lastElement", "()Ljava/lang/Object;",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
*r = (self && !self->refArr.empty())
|
||
|
|
? JVal::MakeRef(self->refArr.back())
|
||
|
|
: JVal::Null();
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(vCls, "removeElementAt", "(I)V",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
if (self && a[1].i >= 0 &&
|
||
|
|
a[1].i < static_cast<int>(self->refArr.size())) {
|
||
|
|
self->refArr.erase(self->refArr.begin() + a[1].i);
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(vCls, "removeElement", "(Ljava/lang/Object;)Z",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
if (self) {
|
||
|
|
auto it = std::find(self->refArr.begin(),
|
||
|
|
self->refArr.end(), a[1].ref);
|
||
|
|
if (it != self->refArr.end()) {
|
||
|
|
self->refArr.erase(it);
|
||
|
|
*r = JVal::MakeInt(1);
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
*r = JVal::MakeInt(0);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(vCls, "removeAllElements", "()V",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
if (self) self->refArr.clear();
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(vCls, "size", "()I",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
*r = JVal::MakeInt(
|
||
|
|
self ? static_cast<int32_t>(self->refArr.size())
|
||
|
|
: 0);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(vCls, "isEmpty", "()Z",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
*r = JVal::MakeInt(
|
||
|
|
(!self || self->refArr.empty()) ? 1 : 0);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(vCls, "contains", "(Ljava/lang/Object;)Z",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
bool found =
|
||
|
|
self && std::find(self->refArr.begin(),
|
||
|
|
self->refArr.end(),
|
||
|
|
a[1].ref) != self->refArr.end();
|
||
|
|
*r = JVal::MakeInt(found ? 1 : 0);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(vCls, "copyInto", "([Ljava/lang/Object;)V",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
JObject* dst = v.getObj(a[1].ref);
|
||
|
|
if (self && dst && dst->arrayKind == ArrayKind::Ref) {
|
||
|
|
size_t n = std::min(self->refArr.size(),
|
||
|
|
dst->refArr.size());
|
||
|
|
for (size_t i = 0; i < n; ++i) {
|
||
|
|
dst->refArr[i] = self->refArr[i];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(vCls, "elements", "()Ljava/util/Enumeration;",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
uint32_t en = v.allocObject("java/util/Enumeration");
|
||
|
|
JObject* eo = v.getObj(en);
|
||
|
|
if (self && eo) {
|
||
|
|
eo->refArr = self->refArr;
|
||
|
|
eo->nativeTag = 0;
|
||
|
|
}
|
||
|
|
*r = JVal::MakeRef(en);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(vCls, "push", "(Ljava/lang/Object;)Ljava/lang/Object;",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
if (self) self->refArr.push_back(a[1].ref);
|
||
|
|
*r = a[1];
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(vCls, "pop", "()Ljava/lang/Object;",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
if (self && !self->refArr.empty()) {
|
||
|
|
*r = JVal::MakeRef(self->refArr.back());
|
||
|
|
self->refArr.pop_back();
|
||
|
|
} else {
|
||
|
|
*r = JVal::Null();
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(vCls, "peek", "()Ljava/lang/Object;",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
*r = (self && !self->refArr.empty())
|
||
|
|
? JVal::MakeRef(self->refArr.back())
|
||
|
|
: JVal::Null();
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(vCls, "empty", "()Z",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
*r = JVal::MakeInt(
|
||
|
|
(!self || self->refArr.empty()) ? 1 : 0);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
// java/util/Enumeration
|
||
|
|
vm.registerNative("java/util/Enumeration", "hasMoreElements", "()Z",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
*r = JVal::MakeInt(
|
||
|
|
(self && self->nativeTag <
|
||
|
|
static_cast<int32_t>(
|
||
|
|
self->refArr.size()))
|
||
|
|
? 1
|
||
|
|
: 0);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/util/Enumeration", "nextElement",
|
||
|
|
"()Ljava/lang/Object;",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
if (self && self->nativeTag <
|
||
|
|
static_cast<int32_t>(
|
||
|
|
self->refArr.size())) {
|
||
|
|
*r = JVal::MakeRef(self->refArr[self->nativeTag++]);
|
||
|
|
} else {
|
||
|
|
*r = JVal::Null();
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
|
||
|
|
// java/util/Hashtable
|
||
|
|
vm.registerNative("java/util/Hashtable", "<init>", "*",
|
||
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) {
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
"java/util/Hashtable", "put",
|
||
|
|
"(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
std::string k = v.getStringUtf8(a[1].ref);
|
||
|
|
if (k.empty()) k = "#" + std::to_string(a[1].ref);
|
||
|
|
*r = self ? self->getField(k, 'L') : JVal::Null();
|
||
|
|
if (self) self->setField(k, a[2]);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
"java/util/Hashtable", "get",
|
||
|
|
"(Ljava/lang/Object;)Ljava/lang/Object;",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
std::string k = v.getStringUtf8(a[1].ref);
|
||
|
|
if (k.empty()) k = "#" + std::to_string(a[1].ref);
|
||
|
|
*r = self ? self->getField(k, 'L') : JVal::Null();
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
"java/util/Hashtable", "containsKey", "(Ljava/lang/Object;)Z",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
std::string k = v.getStringUtf8(a[1].ref);
|
||
|
|
if (k.empty()) k = "#" + std::to_string(a[1].ref);
|
||
|
|
*r = JVal::MakeInt(
|
||
|
|
(self && self->fields.find(k) != self->fields.end()) ? 1 : 0);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/util/Hashtable", "remove",
|
||
|
|
"(Ljava/lang/Object;)Ljava/lang/Object;",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
std::string k = v.getStringUtf8(a[1].ref);
|
||
|
|
if (k.empty()) k = "#" + std::to_string(a[1].ref);
|
||
|
|
*r = self ? self->getField(k, 'L') : JVal::Null();
|
||
|
|
if (self) self->fields.erase(k);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/util/Hashtable", "clear", "()V",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
if (self) self->fields.clear();
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/util/Hashtable", "size", "()I",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
*r = JVal::MakeInt(
|
||
|
|
self ? static_cast<int32_t>(self->fields.size())
|
||
|
|
: 0);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
|
||
|
|
// java/util/Timer & TimerTask
|
||
|
|
vm.registerNative("java/util/Timer", "<init>", "*",
|
||
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) {
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/util/Timer", "schedule", "*",
|
||
|
|
[this](Jvm&, JThread&, const std::vector<JVal>& a, JVal*) {
|
||
|
|
if (a.size() >= 3) {
|
||
|
|
ScheduledTimerTask st;
|
||
|
|
st.taskObjRef = a[1].ref;
|
||
|
|
st.nextRunMs = nowMs() + std::max<int64_t>(0, a[2].l);
|
||
|
|
st.periodMs = (a.size() >= 4) ? a[3].l : 0;
|
||
|
|
timerTasks.push_back(st);
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/util/Timer", "scheduleAtFixedRate", "*",
|
||
|
|
[this](Jvm&, JThread&, const std::vector<JVal>& a, JVal*) {
|
||
|
|
if (a.size() >= 4) {
|
||
|
|
ScheduledTimerTask st;
|
||
|
|
st.taskObjRef = a[1].ref;
|
||
|
|
st.nextRunMs = nowMs() + std::max<int64_t>(0, a[2].l);
|
||
|
|
st.periodMs = std::max<int64_t>(1, a[3].l);
|
||
|
|
timerTasks.push_back(st);
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/util/Timer", "cancel", "()V",
|
||
|
|
[this](Jvm&, JThread&, const std::vector<JVal>&, JVal*) {
|
||
|
|
timerTasks.clear();
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/util/TimerTask", "<init>", "()V",
|
||
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) {
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative("java/util/TimerTask", "cancel", "()Z",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
if (self) self->setField("cancelled", JVal::MakeInt(1));
|
||
|
|
*r = JVal::MakeInt(1);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
void registerJavaIo(Jvm& vm) {
|
||
|
|
// ByteArrayInputStream & DataInputStream & InputStream
|
||
|
|
for (const char* isCls :
|
||
|
|
{"java/io/InputStream", "java/io/ByteArrayInputStream",
|
||
|
|
"java/io/DataInputStream"}) {
|
||
|
|
vm.registerNative(
|
||
|
|
isCls, "<init>", "*",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||
|
|
if (a.size() >= 2 && a[1].type == JType::Ref) {
|
||
|
|
JObject* src = v.getObj(a[1].ref);
|
||
|
|
if (src && src->arrayKind == ArrayKind::ByteOrBool) {
|
||
|
|
auto nis = getOrCreateInputStream(v, a[0].ref);
|
||
|
|
int off = (a.size() >= 4) ? a[2].i : 0;
|
||
|
|
int len = (a.size() >= 4)
|
||
|
|
? a[3].i
|
||
|
|
: static_cast<int>(src->byteArr.size());
|
||
|
|
off = std::clamp(off, 0, static_cast<int>(src->byteArr.size()));
|
||
|
|
len = std::clamp(len, 0,
|
||
|
|
static_cast<int>(src->byteArr.size()) - off);
|
||
|
|
nis->data.assign(
|
||
|
|
reinterpret_cast<const uint8_t*>(src->byteArr.data() + off),
|
||
|
|
reinterpret_cast<const uint8_t*>(src->byteArr.data() + off +
|
||
|
|
len));
|
||
|
|
} else if (src && src->nativeData) {
|
||
|
|
v.getObj(a[0].ref)->nativeData = src->nativeData;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(isCls, "read", "()I",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
auto nis = getOrCreateInputStream(v, a[0].ref);
|
||
|
|
*r = JVal::MakeInt(nis ? nis->readByte() : -1);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
isCls, "read", "([B)I",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
auto nis = getOrCreateInputStream(v, a[0].ref);
|
||
|
|
JObject* arr = v.getObj(a[1].ref);
|
||
|
|
if (!nis || !arr || arr->arrayKind != ArrayKind::ByteOrBool) {
|
||
|
|
*r = JVal::MakeInt(-1);
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
int n = nis->readBytes(
|
||
|
|
reinterpret_cast<uint8_t*>(arr->byteArr.data()),
|
||
|
|
static_cast<int>(arr->byteArr.size()));
|
||
|
|
*r = JVal::MakeInt(n);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
isCls, "read", "([BII)I",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
auto nis = getOrCreateInputStream(v, a[0].ref);
|
||
|
|
JObject* arr = v.getObj(a[1].ref);
|
||
|
|
int off = a[2].i;
|
||
|
|
int len = a[3].i;
|
||
|
|
if (!nis || !arr || arr->arrayKind != ArrayKind::ByteOrBool ||
|
||
|
|
off < 0 || len < 0 ||
|
||
|
|
off + len > static_cast<int>(arr->byteArr.size())) {
|
||
|
|
*r = JVal::MakeInt(-1);
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
int n = nis->readBytes(
|
||
|
|
reinterpret_cast<uint8_t*>(arr->byteArr.data() + off), len);
|
||
|
|
*r = JVal::MakeInt(n);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
isCls, "readFully", "*",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||
|
|
auto nis = getOrCreateInputStream(v, a[0].ref);
|
||
|
|
JObject* arr = v.getObj(a[1].ref);
|
||
|
|
if (!nis || !arr || arr->arrayKind != ArrayKind::ByteOrBool) {
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
int off = (a.size() >= 4) ? a[2].i : 0;
|
||
|
|
int len = (a.size() >= 4) ? a[3].i
|
||
|
|
: static_cast<int>(arr->byteArr.size());
|
||
|
|
off = std::clamp(off, 0, static_cast<int>(arr->byteArr.size()));
|
||
|
|
len = std::clamp(len, 0,
|
||
|
|
static_cast<int>(arr->byteArr.size()) - off);
|
||
|
|
nis->readBytes(
|
||
|
|
reinterpret_cast<uint8_t*>(arr->byteArr.data() + off), len);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(isCls, "available", "()I",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
auto nis = getOrCreateInputStream(v, a[0].ref);
|
||
|
|
*r = JVal::MakeInt(nis ? nis->available() : 0);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(isCls, "skip", "(J)J",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
auto nis = getOrCreateInputStream(v, a[0].ref);
|
||
|
|
if (!nis) {
|
||
|
|
*r = JVal::MakeLong(0);
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
size_t avail = (nis->data.size() > nis->pos)
|
||
|
|
? (nis->data.size() - nis->pos)
|
||
|
|
: 0;
|
||
|
|
size_t sk = std::min<size_t>(
|
||
|
|
avail, static_cast<size_t>(std::max<int64_t>(0, a[1].l)));
|
||
|
|
nis->pos += sk;
|
||
|
|
*r = JVal::MakeLong(static_cast<int64_t>(sk));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(isCls, "skipBytes", "(I)I",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
auto nis = getOrCreateInputStream(v, a[0].ref);
|
||
|
|
if (!nis) {
|
||
|
|
*r = JVal::MakeInt(0);
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
size_t avail = (nis->data.size() > nis->pos)
|
||
|
|
? (nis->data.size() - nis->pos)
|
||
|
|
: 0;
|
||
|
|
size_t sk = std::min<size_t>(
|
||
|
|
avail, static_cast<size_t>(std::max(0, a[1].i)));
|
||
|
|
nis->pos += sk;
|
||
|
|
*r = JVal::MakeInt(static_cast<int32_t>(sk));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(isCls, "mark", "(I)V",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||
|
|
auto nis = getOrCreateInputStream(v, a[0].ref);
|
||
|
|
if (nis) nis->markPos = nis->pos;
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(isCls, "reset", "()V",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||
|
|
auto nis = getOrCreateInputStream(v, a[0].ref);
|
||
|
|
if (nis) nis->pos = nis->markPos;
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(isCls, "close", "()V",
|
||
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) {
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(isCls, "readBoolean", "()Z",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
auto nis = getOrCreateInputStream(v, a[0].ref);
|
||
|
|
*r = JVal::MakeInt((nis && nis->readByte() > 0) ? 1 : 0);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(isCls, "readByte", "()B",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
auto nis = getOrCreateInputStream(v, a[0].ref);
|
||
|
|
*r = JVal::MakeInt(
|
||
|
|
static_cast<int8_t>(nis ? nis->readByte() : 0));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(isCls, "readUnsignedByte", "()I",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
auto nis = getOrCreateInputStream(v, a[0].ref);
|
||
|
|
int b = nis ? nis->readByte() : 0;
|
||
|
|
*r = JVal::MakeInt(b < 0 ? 0 : (b & 0xFF));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(isCls, "readShort", "()S",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
auto nis = getOrCreateInputStream(v, a[0].ref);
|
||
|
|
int b1 = nis ? (nis->readByte() & 0xFF) : 0;
|
||
|
|
int b2 = nis ? (nis->readByte() & 0xFF) : 0;
|
||
|
|
*r = JVal::MakeInt(
|
||
|
|
static_cast<int16_t>((b1 << 8) | b2));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(isCls, "readUnsignedShort", "()I",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
auto nis = getOrCreateInputStream(v, a[0].ref);
|
||
|
|
int b1 = nis ? (nis->readByte() & 0xFF) : 0;
|
||
|
|
int b2 = nis ? (nis->readByte() & 0xFF) : 0;
|
||
|
|
*r = JVal::MakeInt((b1 << 8) | b2);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(isCls, "readChar", "()C",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
auto nis = getOrCreateInputStream(v, a[0].ref);
|
||
|
|
int b1 = nis ? (nis->readByte() & 0xFF) : 0;
|
||
|
|
int b2 = nis ? (nis->readByte() & 0xFF) : 0;
|
||
|
|
*r = JVal::MakeInt((b1 << 8) | b2);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(isCls, "readInt", "()I",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
auto nis = getOrCreateInputStream(v, a[0].ref);
|
||
|
|
uint32_t x = 0;
|
||
|
|
for (int i = 0; i < 4; ++i) {
|
||
|
|
x = (x << 8) |
|
||
|
|
static_cast<uint32_t>(
|
||
|
|
nis ? (nis->readByte() & 0xFF) : 0);
|
||
|
|
}
|
||
|
|
*r = JVal::MakeInt(static_cast<int32_t>(x));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(isCls, "readLong", "()J",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
auto nis = getOrCreateInputStream(v, a[0].ref);
|
||
|
|
uint64_t x = 0;
|
||
|
|
for (int i = 0; i < 8; ++i) {
|
||
|
|
x = (x << 8) |
|
||
|
|
static_cast<uint64_t>(
|
||
|
|
nis ? (nis->readByte() & 0xFF) : 0);
|
||
|
|
}
|
||
|
|
*r = JVal::MakeLong(static_cast<int64_t>(x));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(isCls, "readFloat", "()F",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
auto nis = getOrCreateInputStream(v, a[0].ref);
|
||
|
|
uint32_t x = 0;
|
||
|
|
for (int i = 0; i < 4; ++i) {
|
||
|
|
x = (x << 8) |
|
||
|
|
static_cast<uint32_t>(
|
||
|
|
nis ? (nis->readByte() & 0xFF) : 0);
|
||
|
|
}
|
||
|
|
float fval = 0.0f;
|
||
|
|
std::memcpy(&fval, &x, sizeof(float));
|
||
|
|
*r = JVal::MakeFloat(fval);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
isCls, "readUTF", "()Ljava/lang/String;",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
auto nis = getOrCreateInputStream(v, a[0].ref);
|
||
|
|
int b1 = nis ? (nis->readByte() & 0xFF) : 0;
|
||
|
|
int b2 = nis ? (nis->readByte() & 0xFF) : 0;
|
||
|
|
int len = (b1 << 8) | b2;
|
||
|
|
std::string s(len, '\0');
|
||
|
|
if (nis && len > 0) {
|
||
|
|
nis->readBytes(reinterpret_cast<uint8_t*>(s.data()), len);
|
||
|
|
}
|
||
|
|
*r = JVal::MakeRef(v.allocString(s));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
// OutputStream, ByteArrayOutputStream, DataOutputStream, PrintStream
|
||
|
|
for (const char* osCls :
|
||
|
|
{"java/io/OutputStream", "java/io/ByteArrayOutputStream",
|
||
|
|
"java/io/DataOutputStream", "java/io/PrintStream"}) {
|
||
|
|
vm.registerNative(osCls, "<init>", "*",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||
|
|
if (a.size() >= 2 && a[1].type == JType::Ref) {
|
||
|
|
JObject* sub = v.getObj(a[1].ref);
|
||
|
|
if (sub && sub->nativeData) {
|
||
|
|
v.getObj(a[0].ref)->nativeData = sub->nativeData;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(osCls, "write", "(I)V",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||
|
|
auto nos = getOrCreateOutputStream(v, a[0].ref);
|
||
|
|
if (nos) nos->writeByte(static_cast<uint8_t>(a[1].i));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
osCls, "write", "([B)V",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||
|
|
auto nos = getOrCreateOutputStream(v, a[0].ref);
|
||
|
|
JObject* arr = v.getObj(a[1].ref);
|
||
|
|
if (nos && arr && arr->arrayKind == ArrayKind::ByteOrBool) {
|
||
|
|
nos->writeBytes(
|
||
|
|
reinterpret_cast<const uint8_t*>(arr->byteArr.data()),
|
||
|
|
static_cast<int>(arr->byteArr.size()));
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
osCls, "write", "([BII)V",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||
|
|
auto nos = getOrCreateOutputStream(v, a[0].ref);
|
||
|
|
JObject* arr = v.getObj(a[1].ref);
|
||
|
|
int off = a[2].i;
|
||
|
|
int len = a[3].i;
|
||
|
|
if (nos && arr && arr->arrayKind == ArrayKind::ByteOrBool &&
|
||
|
|
off >= 0 && len > 0 &&
|
||
|
|
off + len <= static_cast<int>(arr->byteArr.size())) {
|
||
|
|
nos->writeBytes(
|
||
|
|
reinterpret_cast<const uint8_t*>(arr->byteArr.data() + off),
|
||
|
|
len);
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
osCls, "toByteArray", "()[B",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
auto nos = getOrCreateOutputStream(v, a[0].ref);
|
||
|
|
int32_t sz = nos ? static_cast<int32_t>(nos->data.size()) : 0;
|
||
|
|
uint32_t arr = v.allocPrimitiveArray(8, sz);
|
||
|
|
if (nos && sz > 0) {
|
||
|
|
std::memcpy(v.getObj(arr)->byteArr.data(), nos->data.data(), sz);
|
||
|
|
}
|
||
|
|
*r = JVal::MakeRef(arr);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(osCls, "size", "()I",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
auto nos = getOrCreateOutputStream(v, a[0].ref);
|
||
|
|
*r = JVal::MakeInt(
|
||
|
|
nos ? static_cast<int32_t>(nos->data.size()) : 0);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(osCls, "reset", "()V",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||
|
|
auto nos = getOrCreateOutputStream(v, a[0].ref);
|
||
|
|
if (nos) nos->data.clear();
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(osCls, "flush", "()V",
|
||
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) {
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(osCls, "close", "()V",
|
||
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) {
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(osCls, "print", "*",
|
||
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) {
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(osCls, "println", "*",
|
||
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) {
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(osCls, "writeBoolean", "(Z)V",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||
|
|
auto nos = getOrCreateOutputStream(v, a[0].ref);
|
||
|
|
if (nos) nos->writeByte(a[1].i != 0 ? 1 : 0);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(osCls, "writeByte", "(I)V",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||
|
|
auto nos = getOrCreateOutputStream(v, a[0].ref);
|
||
|
|
if (nos) nos->writeByte(static_cast<uint8_t>(a[1].i));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(osCls, "writeShort", "(I)V",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||
|
|
auto nos = getOrCreateOutputStream(v, a[0].ref);
|
||
|
|
if (nos) {
|
||
|
|
nos->writeByte(static_cast<uint8_t>(a[1].i >> 8));
|
||
|
|
nos->writeByte(static_cast<uint8_t>(a[1].i));
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(osCls, "writeChar", "(I)V",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||
|
|
auto nos = getOrCreateOutputStream(v, a[0].ref);
|
||
|
|
if (nos) {
|
||
|
|
nos->writeByte(static_cast<uint8_t>(a[1].i >> 8));
|
||
|
|
nos->writeByte(static_cast<uint8_t>(a[1].i));
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(osCls, "writeInt", "(I)V",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||
|
|
auto nos = getOrCreateOutputStream(v, a[0].ref);
|
||
|
|
if (nos) {
|
||
|
|
uint32_t x = static_cast<uint32_t>(a[1].i);
|
||
|
|
nos->writeByte(static_cast<uint8_t>(x >> 24));
|
||
|
|
nos->writeByte(static_cast<uint8_t>(x >> 16));
|
||
|
|
nos->writeByte(static_cast<uint8_t>(x >> 8));
|
||
|
|
nos->writeByte(static_cast<uint8_t>(x));
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(osCls, "writeLong", "(J)V",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||
|
|
auto nos = getOrCreateOutputStream(v, a[0].ref);
|
||
|
|
if (nos) {
|
||
|
|
uint64_t x = static_cast<uint64_t>(a[1].l);
|
||
|
|
for (int i = 7; i >= 0; --i) {
|
||
|
|
nos->writeByte(
|
||
|
|
static_cast<uint8_t>((x >> (i * 8)) & 0xFF));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
osCls, "writeUTF", "(Ljava/lang/String;)V",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||
|
|
auto nos = getOrCreateOutputStream(v, a[0].ref);
|
||
|
|
std::string s = v.getStringUtf8(a[1].ref);
|
||
|
|
if (nos) {
|
||
|
|
uint16_t len = static_cast<uint16_t>(
|
||
|
|
std::min<size_t>(s.size(), 65535));
|
||
|
|
nos->writeByte(static_cast<uint8_t>(len >> 8));
|
||
|
|
nos->writeByte(static_cast<uint8_t>(len));
|
||
|
|
nos->writeBytes(reinterpret_cast<const uint8_t*>(s.data()), len);
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void registerRms(Jvm& vm) {
|
||
|
|
vm.registerNative(
|
||
|
|
"javax/microedition/rms/RecordStore", "openRecordStore", "*",
|
||
|
|
[](Jvm& v, JThread& th, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
std::string name = v.getStringUtf8(a[0].ref);
|
||
|
|
bool create = (a.size() >= 2) ? (a[1].i != 0) : true;
|
||
|
|
if (!v.rms) {
|
||
|
|
v.throwException(th, "javax/microedition/rms/RecordStoreException");
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
RmsStoreData* sd = v.rms->openStore(name, create);
|
||
|
|
if (!sd) {
|
||
|
|
v.throwException(
|
||
|
|
th, "javax/microedition/rms/RecordStoreNotFoundException",
|
||
|
|
name);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
uint32_t ref = v.allocObject("javax/microedition/rms/RecordStore");
|
||
|
|
v.getObj(ref)->strUtf8 = name;
|
||
|
|
*r = JVal::MakeRef(ref);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
"javax/microedition/rms/RecordStore", "closeRecordStore", "()V",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
if (self && v.rms) v.rms->closeStore(self->strUtf8);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
"javax/microedition/rms/RecordStore", "deleteRecordStore",
|
||
|
|
"(Ljava/lang/String;)V",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||
|
|
if (v.rms) v.rms->deleteStore(v.getStringUtf8(a[0].ref));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
"javax/microedition/rms/RecordStore", "listRecordStores",
|
||
|
|
"()[Ljava/lang/String;",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>&, JVal* r) {
|
||
|
|
if (!v.rms) {
|
||
|
|
*r = JVal::Null();
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
auto names = v.rms->listStores();
|
||
|
|
uint32_t arr = v.allocRefArray(
|
||
|
|
"java/lang/String", static_cast<int32_t>(names.size()));
|
||
|
|
for (size_t i = 0; i < names.size(); ++i) {
|
||
|
|
v.getObj(arr)->refArr[i] = v.allocString(names[i]);
|
||
|
|
}
|
||
|
|
*r = JVal::MakeRef(arr);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
"javax/microedition/rms/RecordStore", "getNumRecords", "()I",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
RmsStoreData* sd =
|
||
|
|
(self && v.rms) ? v.rms->openStore(self->strUtf8, true) : nullptr;
|
||
|
|
if (sd) v.rms->closeStore(self->strUtf8);
|
||
|
|
*r = JVal::MakeInt(
|
||
|
|
sd ? static_cast<int32_t>(sd->records.size()) : 0);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
"javax/microedition/rms/RecordStore", "getNextRecordID", "()I",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
RmsStoreData* sd =
|
||
|
|
(self && v.rms) ? v.rms->openStore(self->strUtf8, true) : nullptr;
|
||
|
|
if (sd) v.rms->closeStore(self->strUtf8);
|
||
|
|
*r = JVal::MakeInt(sd ? sd->nextRecordId : 1);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
"javax/microedition/rms/RecordStore", "getSize", "()I",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
RmsStoreData* sd =
|
||
|
|
(self && v.rms) ? v.rms->openStore(self->strUtf8, true) : nullptr;
|
||
|
|
if (sd) v.rms->closeStore(self->strUtf8);
|
||
|
|
*r = JVal::MakeInt((sd && v.rms) ? v.rms->getSize(sd) : 0);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
"javax/microedition/rms/RecordStore", "getSizeAvailable", "()I",
|
||
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal* r) {
|
||
|
|
*r = JVal::MakeInt(4 * 1024 * 1024);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
"javax/microedition/rms/RecordStore", "addRecord", "([BII)I",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
RmsStoreData* sd =
|
||
|
|
(self && v.rms) ? v.rms->openStore(self->strUtf8, true) : nullptr;
|
||
|
|
const uint8_t* ptr = nullptr;
|
||
|
|
int len = 0;
|
||
|
|
if (a[1].ref != 0) {
|
||
|
|
JObject* arr = v.getObj(a[1].ref);
|
||
|
|
if (arr && arr->arrayKind == ArrayKind::ByteOrBool) {
|
||
|
|
int off = std::clamp(
|
||
|
|
a[2].i, 0, static_cast<int>(arr->byteArr.size()));
|
||
|
|
len = std::clamp(
|
||
|
|
a[3].i, 0, static_cast<int>(arr->byteArr.size()) - off);
|
||
|
|
ptr = reinterpret_cast<const uint8_t*>(arr->byteArr.data() + off);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
int32_t id = (sd && v.rms) ? v.rms->addRecord(sd, ptr, len) : 1;
|
||
|
|
if (sd) v.rms->closeStore(self->strUtf8);
|
||
|
|
*r = JVal::MakeInt(id);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
"javax/microedition/rms/RecordStore", "setRecord", "(I[BII)V",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
RmsStoreData* sd =
|
||
|
|
(self && v.rms) ? v.rms->openStore(self->strUtf8, true) : nullptr;
|
||
|
|
const uint8_t* ptr = nullptr;
|
||
|
|
int len = 0;
|
||
|
|
if (a[2].ref != 0) {
|
||
|
|
JObject* arr = v.getObj(a[2].ref);
|
||
|
|
if (arr && arr->arrayKind == ArrayKind::ByteOrBool) {
|
||
|
|
int off = std::clamp(
|
||
|
|
a[3].i, 0, static_cast<int>(arr->byteArr.size()));
|
||
|
|
len = std::clamp(
|
||
|
|
a[4].i, 0, static_cast<int>(arr->byteArr.size()) - off);
|
||
|
|
ptr = reinterpret_cast<const uint8_t*>(arr->byteArr.data() + off);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if (sd && v.rms) {
|
||
|
|
v.rms->setRecord(sd, a[1].i, ptr, len);
|
||
|
|
v.rms->closeStore(self->strUtf8);
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
"javax/microedition/rms/RecordStore", "getRecord", "(I)[B",
|
||
|
|
[](Jvm& v, JThread& th, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
RmsStoreData* sd =
|
||
|
|
(self && v.rms) ? v.rms->openStore(self->strUtf8, true) : nullptr;
|
||
|
|
std::vector<uint8_t> rec;
|
||
|
|
bool ok = sd && v.rms && v.rms->getRecord(sd, a[1].i, &rec);
|
||
|
|
if (sd) v.rms->closeStore(self->strUtf8);
|
||
|
|
if (!ok) {
|
||
|
|
v.throwException(
|
||
|
|
th, "javax/microedition/rms/InvalidRecordIDException");
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
uint32_t arr = v.allocPrimitiveArray(
|
||
|
|
8, static_cast<int32_t>(rec.size()));
|
||
|
|
if (!rec.empty()) {
|
||
|
|
std::memcpy(v.getObj(arr)->byteArr.data(), rec.data(), rec.size());
|
||
|
|
}
|
||
|
|
*r = JVal::MakeRef(arr);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
"javax/microedition/rms/RecordStore", "getRecord", "(I[BI)I",
|
||
|
|
[](Jvm& v, JThread& th, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
RmsStoreData* sd =
|
||
|
|
(self && v.rms) ? v.rms->openStore(self->strUtf8, true) : nullptr;
|
||
|
|
std::vector<uint8_t> rec;
|
||
|
|
bool ok = sd && v.rms && v.rms->getRecord(sd, a[1].i, &rec);
|
||
|
|
if (sd) v.rms->closeStore(self->strUtf8);
|
||
|
|
if (!ok) {
|
||
|
|
v.throwException(
|
||
|
|
th, "javax/microedition/rms/InvalidRecordIDException");
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
JObject* dst = v.getObj(a[2].ref);
|
||
|
|
int off = a[3].i;
|
||
|
|
if (dst && dst->arrayKind == ArrayKind::ByteOrBool && off >= 0 &&
|
||
|
|
off + rec.size() <= dst->byteArr.size()) {
|
||
|
|
std::memcpy(dst->byteArr.data() + off, rec.data(), rec.size());
|
||
|
|
}
|
||
|
|
*r = JVal::MakeInt(static_cast<int32_t>(rec.size()));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
"javax/microedition/rms/RecordStore", "getRecordSize", "(I)I",
|
||
|
|
[](Jvm& v, JThread& th, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
RmsStoreData* sd =
|
||
|
|
(self && v.rms) ? v.rms->openStore(self->strUtf8, true) : nullptr;
|
||
|
|
int32_t sz = (sd && v.rms) ? v.rms->getRecordSize(sd, a[1].i) : -1;
|
||
|
|
if (sd) v.rms->closeStore(self->strUtf8);
|
||
|
|
if (sz < 0) {
|
||
|
|
v.throwException(
|
||
|
|
th, "javax/microedition/rms/InvalidRecordIDException");
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
*r = JVal::MakeInt(sz);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
"javax/microedition/rms/RecordStore", "deleteRecord", "(I)V",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
RmsStoreData* sd =
|
||
|
|
(self && v.rms) ? v.rms->openStore(self->strUtf8, true) : nullptr;
|
||
|
|
if (sd && v.rms) {
|
||
|
|
v.rms->deleteRecord(sd, a[1].i);
|
||
|
|
v.rms->closeStore(self->strUtf8);
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
"javax/microedition/rms/RecordStore", "enumerateRecords", "*",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
RmsStoreData* sd =
|
||
|
|
(self && v.rms) ? v.rms->openStore(self->strUtf8, true) : nullptr;
|
||
|
|
uint32_t en = v.allocObject(
|
||
|
|
"javax/microedition/rms/RecordEnumeration");
|
||
|
|
JObject* eo = v.getObj(en);
|
||
|
|
if (sd && eo) {
|
||
|
|
eo->strUtf8 = self->strUtf8;
|
||
|
|
eo->nativeTag = 0;
|
||
|
|
for (const auto& kv : sd->records) {
|
||
|
|
eo->intArr.push_back(kv.first);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if (sd) v.rms->closeStore(self->strUtf8);
|
||
|
|
*r = JVal::MakeRef(en);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
"javax/microedition/rms/RecordEnumeration", "numRecords", "()I",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
*r = JVal::MakeInt(
|
||
|
|
self ? static_cast<int32_t>(self->intArr.size()) : 0);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
"javax/microedition/rms/RecordEnumeration", "hasNextElement", "()Z",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
*r = JVal::MakeInt(
|
||
|
|
(self && self->nativeTag <
|
||
|
|
static_cast<int32_t>(self->intArr.size()))
|
||
|
|
? 1
|
||
|
|
: 0);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
"javax/microedition/rms/RecordEnumeration", "nextRecordId", "()I",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
int32_t id =
|
||
|
|
(self && self->nativeTag <
|
||
|
|
static_cast<int32_t>(self->intArr.size()))
|
||
|
|
? self->intArr[self->nativeTag++]
|
||
|
|
: 1;
|
||
|
|
*r = JVal::MakeInt(id);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
"javax/microedition/rms/RecordEnumeration", "nextRecord", "()[B",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
if (!self ||
|
||
|
|
self->nativeTag >= static_cast<int32_t>(self->intArr.size()) ||
|
||
|
|
!v.rms) {
|
||
|
|
*r = JVal::Null();
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
int32_t id = self->intArr[self->nativeTag++];
|
||
|
|
RmsStoreData* sd = v.rms->openStore(self->strUtf8, true);
|
||
|
|
std::vector<uint8_t> rec;
|
||
|
|
if (sd) {
|
||
|
|
v.rms->getRecord(sd, id, &rec);
|
||
|
|
v.rms->closeStore(self->strUtf8);
|
||
|
|
}
|
||
|
|
uint32_t arr = v.allocPrimitiveArray(
|
||
|
|
8, static_cast<int32_t>(rec.size()));
|
||
|
|
if (!rec.empty()) {
|
||
|
|
std::memcpy(v.getObj(arr)->byteArr.data(), rec.data(), rec.size());
|
||
|
|
}
|
||
|
|
*r = JVal::MakeRef(arr);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
"javax/microedition/rms/RecordEnumeration", "destroy", "()V",
|
||
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) { return true; });
|
||
|
|
}
|
||
|
|
|
||
|
|
static void executeHttpIfNeeded(NativeConnection* nc) {
|
||
|
|
if (!nc || nc->kind != NativeConnection::Http || nc->executed) return;
|
||
|
|
nc->executed = true;
|
||
|
|
nc->inStream = std::make_shared<NativeInputStream>();
|
||
|
|
|
||
|
|
CURL* curl = curl_easy_init();
|
||
|
|
if (!curl) {
|
||
|
|
nc->responseCode = 500;
|
||
|
|
nc->responseMessage = "CURL Error";
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
curl_easy_setopt(curl, CURLOPT_URL, nc->url.c_str());
|
||
|
|
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
|
||
|
|
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 15L);
|
||
|
|
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 8L);
|
||
|
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
|
||
|
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
|
||
|
|
curl_easy_setopt(
|
||
|
|
curl, CURLOPT_USERAGENT,
|
||
|
|
"Nokia8110-4G/2.0 (Bananarchy; ReMEdy/1.0) Profile/MIDP-2.0 Configuration/CLDC-1.1");
|
||
|
|
|
||
|
|
struct curl_slist* headers = nullptr;
|
||
|
|
for (const auto& kv : nc->reqHeaders) {
|
||
|
|
std::string h = kv.first + ": " + kv.second;
|
||
|
|
headers = curl_slist_append(headers, h.c_str());
|
||
|
|
}
|
||
|
|
if (headers) curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
|
||
|
|
|
||
|
|
if (nc->method == "POST") {
|
||
|
|
curl_easy_setopt(curl, CURLOPT_POST, 1L);
|
||
|
|
if (nc->outStream && !nc->outStream->data.empty()) {
|
||
|
|
curl_easy_setopt(curl, CURLOPT_POSTFIELDS,
|
||
|
|
nc->outStream->data.data());
|
||
|
|
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE,
|
||
|
|
static_cast<long>(nc->outStream->data.size()));
|
||
|
|
} else {
|
||
|
|
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, 0L);
|
||
|
|
}
|
||
|
|
} else if (nc->method == "HEAD") {
|
||
|
|
curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
|
||
|
|
}
|
||
|
|
|
||
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlWriteVector);
|
||
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &nc->inStream->data);
|
||
|
|
|
||
|
|
CURLcode res = curl_easy_perform(curl);
|
||
|
|
long code = 200;
|
||
|
|
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
|
||
|
|
if (headers) curl_slist_free_all(headers);
|
||
|
|
curl_easy_cleanup(curl);
|
||
|
|
|
||
|
|
if (res != CURLE_OK) {
|
||
|
|
nc->responseCode = 503;
|
||
|
|
nc->responseMessage = "Network Unreachable";
|
||
|
|
} else {
|
||
|
|
nc->responseCode = static_cast<int>(code > 0 ? code : 200);
|
||
|
|
nc->responseMessage = (nc->responseCode == 200) ? "OK" : "HTTP Response";
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void registerGcfNetwork(Jvm& vm) {
|
||
|
|
auto openConn = [this](Jvm& v, JThread& th, const std::string& url) -> uint32_t {
|
||
|
|
if (!networkEnabled && url.rfind("file://", 0) != 0) {
|
||
|
|
v.throwException(th, "java/io/IOException", "Network disabled in ReMEdy");
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
auto nc = std::make_shared<NativeConnection>();
|
||
|
|
nc->url = url;
|
||
|
|
nc->inStream = std::make_shared<NativeInputStream>();
|
||
|
|
nc->outStream = std::make_shared<NativeOutputStream>();
|
||
|
|
|
||
|
|
if (url.rfind("socket://", 0) == 0 || url.rfind("ssl://", 0) == 0 ||
|
||
|
|
url.rfind("tls://", 0) == 0) {
|
||
|
|
nc->kind = NativeConnection::Socket;
|
||
|
|
size_t schemeEnd = url.find("://") + 3;
|
||
|
|
std::string hostPort = url.substr(schemeEnd);
|
||
|
|
size_t colon = hostPort.rfind(':');
|
||
|
|
nc->host = (colon == std::string::npos) ? hostPort
|
||
|
|
: hostPort.substr(0, colon);
|
||
|
|
nc->port = (colon == std::string::npos)
|
||
|
|
? 80
|
||
|
|
: std::atoi(hostPort.substr(colon + 1).c_str());
|
||
|
|
if (nc->host.empty()) nc->host = "127.0.0.1";
|
||
|
|
|
||
|
|
struct addrinfo hints {}, *res = nullptr;
|
||
|
|
hints.ai_family = AF_INET;
|
||
|
|
hints.ai_socktype = SOCK_STREAM;
|
||
|
|
std::string portStr = std::to_string(nc->port);
|
||
|
|
if (::getaddrinfo(nc->host.c_str(), portStr.c_str(), &hints, &res) == 0 &&
|
||
|
|
res) {
|
||
|
|
int fd = ::socket(res->ai_family, res->ai_socktype, res->ai_protocol);
|
||
|
|
if (fd >= 0) {
|
||
|
|
struct timeval tv {4, 0};
|
||
|
|
::setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
|
||
|
|
::setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
|
||
|
|
if (::connect(fd, res->ai_addr, res->ai_addrlen) == 0) {
|
||
|
|
nc->sockFd = fd;
|
||
|
|
nc->inStream->sockFd = fd;
|
||
|
|
nc->outStream->sockFd = fd;
|
||
|
|
} else {
|
||
|
|
::close(fd);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
::freeaddrinfo(res);
|
||
|
|
}
|
||
|
|
uint32_t ref = v.allocObject("javax/microedition/io/SocketConnection");
|
||
|
|
v.getObj(ref)->nativeData = nc;
|
||
|
|
return ref;
|
||
|
|
} else if (url.rfind("http://", 0) == 0 ||
|
||
|
|
url.rfind("https://", 0) == 0) {
|
||
|
|
nc->kind = NativeConnection::Http;
|
||
|
|
uint32_t ref = v.allocObject("javax/microedition/io/HttpConnection");
|
||
|
|
v.getObj(ref)->nativeData = nc;
|
||
|
|
return ref;
|
||
|
|
} else {
|
||
|
|
nc->kind = NativeConnection::File;
|
||
|
|
uint32_t ref = v.allocObject(
|
||
|
|
"javax/microedition/io/file/FileConnection");
|
||
|
|
v.getObj(ref)->nativeData = nc;
|
||
|
|
return ref;
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
vm.registerNative(
|
||
|
|
"javax/microedition/io/Connector", "open", "*",
|
||
|
|
[openConn](Jvm& v, JThread& th, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
std::string url = v.getStringUtf8(a[0].ref);
|
||
|
|
uint32_t cref = openConn(v, th, url);
|
||
|
|
if (cref == 0) return false;
|
||
|
|
*r = JVal::MakeRef(cref);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
"javax/microedition/io/Connector", "openInputStream",
|
||
|
|
"(Ljava/lang/String;)Ljava/io/InputStream;",
|
||
|
|
[openConn](Jvm& v, JThread& th, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
uint32_t cref = openConn(v, th, v.getStringUtf8(a[0].ref));
|
||
|
|
if (cref == 0) return false;
|
||
|
|
auto* nc = static_cast<NativeConnection*>(
|
||
|
|
v.getObj(cref)->nativeData.get());
|
||
|
|
executeHttpIfNeeded(nc);
|
||
|
|
uint32_t isRef = v.allocObject("java/io/DataInputStream");
|
||
|
|
v.getObj(isRef)->nativeData = nc->inStream;
|
||
|
|
*r = JVal::MakeRef(isRef);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
"javax/microedition/io/Connector", "openDataInputStream",
|
||
|
|
"(Ljava/lang/String;)Ljava/io/DataInputStream;",
|
||
|
|
[openConn](Jvm& v, JThread& th, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
uint32_t cref = openConn(v, th, v.getStringUtf8(a[0].ref));
|
||
|
|
if (cref == 0) return false;
|
||
|
|
auto* nc = static_cast<NativeConnection*>(
|
||
|
|
v.getObj(cref)->nativeData.get());
|
||
|
|
executeHttpIfNeeded(nc);
|
||
|
|
uint32_t isRef = v.allocObject("java/io/DataInputStream");
|
||
|
|
v.getObj(isRef)->nativeData = nc->inStream;
|
||
|
|
*r = JVal::MakeRef(isRef);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
"javax/microedition/io/Connector", "openOutputStream",
|
||
|
|
"(Ljava/lang/String;)Ljava/io/OutputStream;",
|
||
|
|
[openConn](Jvm& v, JThread& th, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
uint32_t cref = openConn(v, th, v.getStringUtf8(a[0].ref));
|
||
|
|
if (cref == 0) return false;
|
||
|
|
auto* nc = static_cast<NativeConnection*>(
|
||
|
|
v.getObj(cref)->nativeData.get());
|
||
|
|
uint32_t osRef = v.allocObject("java/io/DataOutputStream");
|
||
|
|
v.getObj(osRef)->nativeData = nc->outStream;
|
||
|
|
*r = JVal::MakeRef(osRef);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
|
||
|
|
for (const char* connCls :
|
||
|
|
{"javax/microedition/io/Connection",
|
||
|
|
"javax/microedition/io/InputConnection",
|
||
|
|
"javax/microedition/io/OutputConnection",
|
||
|
|
"javax/microedition/io/StreamConnection",
|
||
|
|
"javax/microedition/io/ContentConnection",
|
||
|
|
"javax/microedition/io/SocketConnection",
|
||
|
|
"javax/microedition/io/HttpConnection",
|
||
|
|
"javax/microedition/io/HttpsConnection",
|
||
|
|
"javax/microedition/io/file/FileConnection"}) {
|
||
|
|
vm.registerNative(
|
||
|
|
connCls, "openInputStream", "()Ljava/io/InputStream;",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
auto* nc = self ? static_cast<NativeConnection*>(
|
||
|
|
self->nativeData.get())
|
||
|
|
: nullptr;
|
||
|
|
executeHttpIfNeeded(nc);
|
||
|
|
uint32_t isRef = v.allocObject("java/io/DataInputStream");
|
||
|
|
if (nc) v.getObj(isRef)->nativeData = nc->inStream;
|
||
|
|
*r = JVal::MakeRef(isRef);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
connCls, "openDataInputStream", "()Ljava/io/DataInputStream;",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
auto* nc = self ? static_cast<NativeConnection*>(
|
||
|
|
self->nativeData.get())
|
||
|
|
: nullptr;
|
||
|
|
executeHttpIfNeeded(nc);
|
||
|
|
uint32_t isRef = v.allocObject("java/io/DataInputStream");
|
||
|
|
if (nc) v.getObj(isRef)->nativeData = nc->inStream;
|
||
|
|
*r = JVal::MakeRef(isRef);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
connCls, "openOutputStream", "()Ljava/io/OutputStream;",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
auto* nc = self ? static_cast<NativeConnection*>(
|
||
|
|
self->nativeData.get())
|
||
|
|
: nullptr;
|
||
|
|
uint32_t osRef = v.allocObject("java/io/DataOutputStream");
|
||
|
|
if (nc) v.getObj(osRef)->nativeData = nc->outStream;
|
||
|
|
*r = JVal::MakeRef(osRef);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
connCls, "openDataOutputStream", "()Ljava/io/DataOutputStream;",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
auto* nc = self ? static_cast<NativeConnection*>(
|
||
|
|
self->nativeData.get())
|
||
|
|
: nullptr;
|
||
|
|
uint32_t osRef = v.allocObject("java/io/DataOutputStream");
|
||
|
|
if (nc) v.getObj(osRef)->nativeData = nc->outStream;
|
||
|
|
*r = JVal::MakeRef(osRef);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
connCls, "setRequestMethod", "(Ljava/lang/String;)V",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
auto* nc = self ? static_cast<NativeConnection*>(
|
||
|
|
self->nativeData.get())
|
||
|
|
: nullptr;
|
||
|
|
if (nc) nc->method = v.getStringUtf8(a[1].ref);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
connCls, "setRequestProperty",
|
||
|
|
"(Ljava/lang/String;Ljava/lang/String;)V",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
auto* nc = self ? static_cast<NativeConnection*>(
|
||
|
|
self->nativeData.get())
|
||
|
|
: nullptr;
|
||
|
|
if (nc) {
|
||
|
|
nc->reqHeaders[v.getStringUtf8(a[1].ref)] =
|
||
|
|
v.getStringUtf8(a[2].ref);
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
connCls, "getResponseCode", "()I",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
auto* nc = self ? static_cast<NativeConnection*>(
|
||
|
|
self->nativeData.get())
|
||
|
|
: nullptr;
|
||
|
|
executeHttpIfNeeded(nc);
|
||
|
|
*r = JVal::MakeInt(nc ? nc->responseCode : 200);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
connCls, "getResponseMessage", "()Ljava/lang/String;",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
auto* nc = self ? static_cast<NativeConnection*>(
|
||
|
|
self->nativeData.get())
|
||
|
|
: nullptr;
|
||
|
|
executeHttpIfNeeded(nc);
|
||
|
|
*r = JVal::MakeRef(
|
||
|
|
v.allocString(nc ? nc->responseMessage : "OK"));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
connCls, "getLength", "()J",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
auto* nc = self ? static_cast<NativeConnection*>(
|
||
|
|
self->nativeData.get())
|
||
|
|
: nullptr;
|
||
|
|
executeHttpIfNeeded(nc);
|
||
|
|
int64_t len = (nc && nc->inStream)
|
||
|
|
? static_cast<int64_t>(nc->inStream->data.size())
|
||
|
|
: -1;
|
||
|
|
*r = JVal::MakeLong(len);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
connCls, "getAddress", "()Ljava/lang/String;",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
auto* nc = self ? static_cast<NativeConnection*>(
|
||
|
|
self->nativeData.get())
|
||
|
|
: nullptr;
|
||
|
|
*r = JVal::MakeRef(v.allocString(nc ? nc->host : "127.0.0.1"));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
connCls, "getPort", "()I",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
auto* nc = self ? static_cast<NativeConnection*>(
|
||
|
|
self->nativeData.get())
|
||
|
|
: nullptr;
|
||
|
|
*r = JVal::MakeInt(nc ? nc->port : 80);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
connCls, "setSocketOption", "(BI)V",
|
||
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) {
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
connCls, "close", "()V",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
auto* nc = self ? static_cast<NativeConnection*>(
|
||
|
|
self->nativeData.get())
|
||
|
|
: nullptr;
|
||
|
|
if (nc) nc->close();
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void registerMmapi(Jvm& vm) {
|
||
|
|
vm.registerNative(
|
||
|
|
"javax/microedition/media/Manager", "playTone", "(III)V",
|
||
|
|
[this](Jvm&, JThread&, const std::vector<JVal>& a, JVal*) {
|
||
|
|
sound.playTone(a[0].i, a[1].i, a[2].i);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
"javax/microedition/media/Manager", "createPlayer", "*",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
uint32_t pref = v.allocObject("javax/microedition/media/Player");
|
||
|
|
JObject* po = v.getObj(pref);
|
||
|
|
if (po && !a.empty() && a[0].type == JType::Ref && a[0].ref != 0) {
|
||
|
|
JObject* src = v.getObj(a[0].ref);
|
||
|
|
if (src && src->nativeData) {
|
||
|
|
po->nativeData = src->nativeData;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
*r = JVal::MakeRef(pref);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
|
||
|
|
for (const char* pCls :
|
||
|
|
{"javax/microedition/media/Player",
|
||
|
|
"javax/microedition/media/Controllable",
|
||
|
|
"javax/microedition/media/control/VolumeControl",
|
||
|
|
"com/nokia/mid/sound/Sound"}) {
|
||
|
|
vm.registerNative(pCls, "<init>", "*",
|
||
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) {
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(pCls, "realize", "()V",
|
||
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) {
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(pCls, "prefetch", "()V",
|
||
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) {
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
pCls, "start", "()V",
|
||
|
|
[this](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
if (self && self->nativeData) {
|
||
|
|
auto* nis = static_cast<NativeInputStream*>(
|
||
|
|
self->nativeData.get());
|
||
|
|
sound.playWavBytes(nis->data, self->nativeTag < 0);
|
||
|
|
} else {
|
||
|
|
sound.playTone(69, 90, 75);
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
pCls, "play", "(I)V",
|
||
|
|
[this](Jvm&, JThread&, const std::vector<JVal>&, JVal*) {
|
||
|
|
sound.playTone(72, 80, 80);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
pCls, "stop", "()V",
|
||
|
|
[this](Jvm&, JThread&, const std::vector<JVal>&, JVal*) {
|
||
|
|
sound.stopAll();
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(pCls, "deallocate", "()V",
|
||
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) {
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(pCls, "close", "()V",
|
||
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) {
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
pCls, "setLoopCount", "(I)V",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||
|
|
JObject* self = v.getObj(a[0].ref);
|
||
|
|
if (self) self->nativeTag = a[1].i;
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
pCls, "getControl",
|
||
|
|
"(Ljava/lang/String;)Ljavax/microedition/media/Control;",
|
||
|
|
[](Jvm& v, JThread&, const std::vector<JVal>&, JVal* r) {
|
||
|
|
*r = JVal::MakeRef(v.allocObject(
|
||
|
|
"javax/microedition/media/control/VolumeControl"));
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
pCls, "setLevel", "(I)I",
|
||
|
|
[this](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||
|
|
sound.volumePct = std::clamp(a[1].i, 0, 100);
|
||
|
|
*r = JVal::MakeInt(sound.volumePct);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
vm.registerNative(
|
||
|
|
pCls, "getLevel", "()I",
|
||
|
|
[this](Jvm&, JThread&, const std::vector<JVal>&, JVal* r) {
|
||
|
|
*r = JVal::MakeInt(sound.volumePct);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
} // namespace remedy
|
||
|
|
|
||
|
|
#endif // BANANARCHY_REMEDY_CLDC_NET_MEDIA_HPP
|